首页 > 代码库 > POJ 1979 Red and Black
POJ 1979 Red and Black
http://poj.org/problem?id=1979
方法和Lake Counting 完全一样
1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 //思路与Lake counting 完全一样 6 const int maxsize = 128; 7 int M,N,cnt; 8 char room[maxsize][maxsize]; 9 int d[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; 10 11 bool check(int x, int y) 12 { 13 if (x < 0 || x >= N || y < 0 || y >= M) return false; 14 if (room[x][y] == ‘#‘) return false; 15 return true; 16 } 17 18 void dfs(int x, int y) 19 { 20 int nx, ny; 21 room[x][y] = ‘#‘;//将这个格置为# 22 for (int i = 0; i < 4; i++) 23 { 24 nx = x + d[0][i]; 25 ny = y + d[1][i]; 26 if (check(nx, ny)) 27 { 28 cnt++; 29 dfs(nx, ny); 30 } 31 } 32 } 33 34 int main() 35 { 36 int x,y; 37 freopen("in.txt", "r", stdin); 38 while(~scanf("%d%d", &M, &N)) 39 { 40 if (M == 0 && N == 0) break; 41 for (int i = 0; i < N; i++) 42 { 43 getchar(); 44 for (int j = 0; j < M; j++) 45 { 46 scanf("%c", &room[i][j]); 47 if (room[i][j] == ‘@‘) 48 { 49 x = i; 50 y = j; 51 } 52 } 53 } 54 cnt = 1; 55 dfs(x, y); 56 printf("%d\n", cnt ); 57 } 58 return 0; 59 }
POJ 1979 Red and Black
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。