This problem is very simple, but I do not know why the operation results have been wrong, but the submission is right! The code is really magical, but I guess it might be that the data given is wrong, maybe the data given above M and N are given a space or carriage return, but the data of the problem is not
#include <stdio.h> #include <string.h> #define N 110int m, N;char maps[n][n];int dir[8][2]={{-1,-1}, {-1, 0}, { -1, 1}, {0,-1}, {0, 1}, {1,-1}, {1, 0}, {1, 1}};void DFS (int x, int y), int main () {while (scanf ("%d%d", &m, &n ), m!=0) {int ans=0; for (int i=0; i<m; i++) {GetChar (); for (int j=0; j<n; J + +) {scanf ("%c", &maps[i][j]); }} for (int i=0, i<m; i++) for (int j=0; j<n; J + +) {if (Maps[i][j] = = ' @ ') {DFS (i, j); ans++; }} printf ("%d\n", ans); }}void DFS (int x, int y) {maps[x][y]= ' * '; for (int i=0; i<8; i++) {int nx=x+dir[i][0]; int ny=y+dir[i][1]; if (maps[nx][ny]== ' @ ' &&nx>=0&&nx<m&&ny>=0&&ny<n) DFS (NX, NY); }}
HDU 1241 Oilfield