• hdu 1312 Red and Black


      这道是搜索水题,dfs,bfs都可以,一开始我用dfs,可是竟然出了很多错误,后来看清样例才发现题目原来是先输入列再输入行的,我说有必要这样卡别人么,然后深搜测试样例时也爆栈了(毕竟搜索类的题目基本没碰过),要记得每次访问完某个结点都要做标记的!(不然就无限递归了~)这道题我对边界都做了标记,所以深搜时免去了对于边界的繁琐的判断:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 char c[23][23];
     7 int n,m;
     8 
     9 int dfs(int i, int j)
    10 {
    11     if(c[i][j]=='#')    return 0;
    12     c[i][j]= '#';
    13     return  dfs(i-1,j)+dfs(i+1,j)+dfs(i,j-1)+dfs(i,j+1)+1;
    14 }
    15 
    16 int main(){
    17     int i,j,ti,tj;
    18     while(~scanf("%d%d",&m,&n) && m){
    19         getchar();
    20         memset(c,'#',sizeof(c));
    21         for(i=1; i<=n; ++i,getchar())
    22             for(j=1; j<=m; ++j)
    23                 if((c[i][j]= getchar())=='@')    ti= i,    tj= j;    
    24         printf("%d
    ",dfs(ti,tj));
    25     }
    26     return 0;
    27 }
    View Code

      看了别人的讨论后,发现bfs也可以做,同样的一定要对已访问过的结点做标记!(就是在这儿卡了好久,幸好用了一种奇葩的调试方法,能够看清每次入队的结点,见注释~):

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 char s[23][23];
     9 
    10 struct node{
    11     int x,y;
    12     node(int x= 0, int y= 0): x(x),y(y) {}
    13 };
    14 
    15 int main(){
    16     int n,m,ti,tj;
    17     while(~scanf("%d%d",&m,&n),m){
    18         memset(s,'#',sizeof(s));
    19         getchar();
    20         for(int i=1; i<=n; ++i,getchar())
    21             for(int j=1; j<=m; ++j)
    22                 if((s[i][j]= getchar())=='@')    ti= i,    tj= j;
    23         int ans= 0;
    24         queue<node> q;
    25         q.push(node(ti,tj));
    26         s[ti][tj]= '#';
    27         while(!q.empty()){
    28             node t= q.front();    q.pop();
    29             if(s[t.x-1][t.y]=='.'){
    30                 q.push(node(t.x-1,t.y));
    31                 s[t.x-1][t.y]= '#';
    32         //        printf("%d %d
    ",t.x-1, t.y);
    33             }
    34             if(s[t.x+1][t.y]=='.'){
    35                 q.push(node(t.x+1,t.y));
    36                 s[t.x+1][t.y]= '#';
    37         //        printf("%d %d
    ",t.x+1, t.y);
    38             }
    39             if(s[t.x][t.y-1]=='.'){
    40                 q.push(node(t.x,t.y-1));
    41                 s[t.x][t.y-1]= '#';
    42         //        printf("%d %d
    ",t.x, t.y-1);
    43             }
    44             if(s[t.x][t.y+1]=='.'){
    45                 q.push(node(t.x,t.y+1));
    46                 s[t.x][t.y+1]= '#';
    47         //        printf("%d %d
    ",t.x, t.y+1);
    48             }
    49             ++ans;
    50         //    system("pause");
    51         }
    52         printf("%d
    ",ans);
    53     }
    54     return 0;
    55 }
    View Code

       还是写整洁一点吧:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 char s[23][23];
     9 int dir[2][4]= {{-1,0,0,1},{0,-1,1,0}};
    10 
    11 struct node{
    12     int x,y;
    13     node(int x= 0, int y= 0): x(x),y(y) {}
    14 };
    15 
    16 int main(){
    17     int n,m,ti,tj;
    18     while(~scanf("%d%d",&m,&n),m){
    19         memset(s,'#',sizeof(s));
    20         getchar();
    21         for(int i=1; i<=n; ++i,getchar())
    22             for(int j=1; j<=m; ++j)
    23                 if((s[i][j]= getchar())=='@')    ti= i,    tj= j;
    24         int ans= 0;
    25         queue<node> q;
    26         q.push(node(ti,tj));
    27         s[ti][tj]= '#';
    28         while(!q.empty()){
    29             node t= q.front();    q.pop();
    30             for(int k=0; k<4; ++k){
    31                 int dx= t.x+dir[0][k], dy= t.y+dir[1][k];
    32                 if(s[dx][dy]=='.'){
    33                     s[dx][dy]= '#';
    34                     q.push(node(dx,dy));
    35                 }
    36             }
    37             ++ans;
    38         }
    39         printf("%d
    ",ans);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    【转】【人在职场】能力与价值
    【转】小议Bug敏感度---Bug敏感度的故事(一)
    【转】请尊重测试:测试从业五年有感
    【转】性能测试设计和LR原理的探讨
    【转】企业级Java应用最重要的4个性能指标
    暂存
    存储过程参数输入输出
    sqlserver 分页
    学习网址
    daatable动态创建
  • 原文地址:https://www.cnblogs.com/Newdawn/p/4379546.html
Copyright © 2020-2023  润新知