ACM PKU 1562 Oil Deposits
题目链接:http://poj.org/problem?id=1562
这道题相当尴尬了,两个一样的程序换个输入就A了,改成一个个字母读入就wa了,不知道为什么,希望大家能指出错误之处,不胜感激;
AC的代码:
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=100; char ch[maxn+10][maxn+10]; int r[8]= {0,-1,-1,-1,0,1,1,1},c[8]= {1,1,0,-1,-1,-1,0,1}; int m,n,ans; bool charge(int x,int y) { if(x<0||y<0||x>=m||y>=n||ch[x][y]=='*') return false; return true; } void dfs(int x,int y) { ch[x][y]='*'; for(int i=0; i<8; i++) { int a=x+r[i]; int b=y+c[i]; if(charge(a,b)) dfs(a,b); } return; } int main() { //freopen("in.txt","r",stdin); while(scanf("%d%d",&m,&n)&&m) { ans=0; getchar(); for(int i=0; i<m; i++) { scanf("%s",ch[i]); } for(int i=0; i<m; i++) for(int j=0; j<n; j++) if(ch[i][j]=='@') { dfs(i,j); ans++; } printf("%d\n",ans); } return 0; }
wa了的代码:
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=100; char ch[maxn+10][maxn+10]; int r[8]= {0,-1,-1,-1,0,1,1,1},c[8]= {1,1,0,-1,-1,-1,0,1}; int m,n,ans; bool charge(int x,int y) { if(x<0||y<0||x>=m||y>=n||ch[x][y]=='*') return false; return true; } void dfs(int x,int y) { ch[x][y]='*'; for(int i=0; i<8; i++) { int a=x+r[i]; int b=y+c[i]; if(charge(a,b)) dfs(a,b); } return; } int main() { // freopen("in.txt","r",stdin); while(scanf("%d%d",&m,&n)&&m) { ans=0; getchar(); for(int i=0; i<m; i++) { for(int j=0;j<n;j++) scanf("%c",&ch[i][j]); getchar(); } for(int i=0; i<m; i++) for(int j=0; j<n; j++) if(ch[i][j]=='@') { dfs(i,j); ans++; } printf("%d\n",ans); } return 0; }