Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
第一次做搜索 帖下码吧#include <iostream> #include <cstdlib> using namespace std; int map[22][22]; struct POS { int x, y; }; POS pos; int h, w; int step; int mov[4][2] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} }; void dfs(POS now) { map[now.x][now.y] = 1; for (int i = 0; i < 4; ++i) { int tx = now.x + mov[i][0]; int ty = now.y + mov[i][1]; if (tx >= 0 && tx < w && ty >= 0 && ty < h && map[tx][ty] == 0) { POS tmp; tmp.x = tx; tmp.y = ty; step++; dfs(tmp); } } } int main(int argc, char** argv) { while (scanf("%d%d", &w, &h) == 2) { step = 1; if (w == 0 && h == 0)break; getchar(); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { char c; cin >> c; if (c == '.') map[j][i] = 0; else if (c == '#')map[j][i] = 1; else { map[j][i] == 1; pos.x = j; pos.y = i; } } getchar(); } dfs(pos); printf("%d\n", step); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator