Problem Address:http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=1012
目前只会使用简单的DFS。
由于打错了一个字母,结果WA了几次。
贴下代码。
#include <iostream>using namespace std;char ch[101][101];int m,n;int ct;void set(int i, int j){ if (ch[i][j]=='*') return; ch[i][j] = '*'; if (i!=0) { set(i-1,j); if (j!=0) set(i-1,j-1); if (j+1!=n) set(i-1,j+1); } if (j!=0) set(i,j-1); if (j+1!=n) set(i,j+1); if (i+1!=m) { set(i+1,j); if (j!=0) set(i+1,j-1); if (j+1!=n) set(i+1,j+1); }}int main(){ int i,j; while(scanf("%d %d", &m, &n)) { if (m==0) break; cin.get(); ct = 0; for (i=0; i<m; i++) { scanf("%s", ch[i]); } for (i=0; i<m; i++) { for (j=0; j<n; j++) { if (ch[i][j]=='@') { ct++; set(i,j); } } } printf("%d/n", ct); } return 0;}