Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41107
  • 博文数量: 37
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 372
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-12 23:27
文章分类

全部博文(37)

文章存档

2014年(5)

2013年(32)

我的朋友

分类: C/C++

2013-11-20 14:49:54

遍历,遇到O,则push到数组,然后搜寻附近的O,如果遇到边界的O,则表明该区域不是封闭的;如果最后不再找到O,说明该区域封闭,将该区域内元素全改为X。每次处理一个O后都将其改为.,避免重复处理。最后需要将.还原成O。代码如下


点击(此处)折叠或打开

  1. class Solution {
  2. public:
  3.    
  4.     void solve(vector<vector<char>> &board) {
  5.         // IMPORTANT: Please reset any member data you declared, as
  6.         // the same Solution instance will be reused for each test case.
  7.         vector<vector<int>> region;
  8.        
  9.         if(board.size()<2||board[0].size()<2) return;
  10.        
  11.         for(int i=1;i<board[0].size()-1;i++)
  12.         {
  13.             for(int j=1;j<board.size()-1;j++)
  14.             {
  15.                 if(board[j][i]=='O')
  16.                 {
  17.                     board[j][i]='.';
  18.                     region.clear();
  19.                     region.push_back(vector<int>(2,j));
  20.                     region[region.size()-1][1]=i;
  21.                     bool flag=true;
  22.                     int oldlen=0;
  23.                     int newlen=region.size();
  24.                     while(oldlen!=newlen)
  25.                     {
  26.                         for(int k=oldlen;k<newlen;k++)
  27.                         {
  28.                             int row=region[k][0],coloum=region[k][1];
  29.                             if(row==0||row==board.size()-1||coloum==0||coloum==board[0].size()-1) {flag=false;};
  30.                             if(row>0&&board[row-1][coloum]=='O')
  31.                                  {region.push_back(vector<int>(2,row-1));region[region.size()-1][1]=coloum;board[row-1][coloum]='.';}
  32.                             if(row<board.size()-1&&board[row+1][coloum]=='O')
  33.                                  {region.push_back(vector<int>(2,row+1));region[region.size()-1][1]=coloum;board[row+1][coloum]='.';}
  34.                             if(coloum>0&&board[row][coloum-1]=='O')
  35.                                  {region.push_back(vector<int>(2,row));region[region.size()-1][1]=coloum-1;board[row][coloum-1]='.';}
  36.                             if(coloum<board[0].size()-1&&board[row][coloum+1]=='O')
  37.                                  {region.push_back(vector<int>(2,row));region[region.size()-1][1]=coloum+1;board[row][coloum+1]='.';}
  38.                         }
  39.                         oldlen=newlen;
  40.                         newlen=region.size();
  41.                     }
  42.                     if(true==flag)
  43.                     {
  44.                         for(int k=0;k<region.size();k++)
  45.                         {
  46.                             int row=region[k][0], coloum=region[k][1];
  47.                             board[row][coloum]='X';
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         for(int i=0;i<board[0].size();i++)
  54.         {
  55.             for(int j=0;j<board.size();j++)
  56.             {
  57.                 if(board[j][i]=='.')
  58.                 {
  59.                     board[j][i]='O';
  60.                 }
  61.             }
  62.         }
  63.     }
  64. };

阅读(210) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~