Source Code
Problem: 1979 | User: henry11 | |
Memory: 356K | Time: 0MS | |
Language: G++ | Result: Accepted |
- Source Code
/* 廣度搜索 queue 為訪問堆; status 表示訪問狀態; map 表示地圖; */ #include<stdio.h> #include<string.h> struct node { int a, b; }; int direct[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; node queue[401]; char map[21][21]; int head, tail; int count, len, height; void initQueue(int x, int y) { queue[0].a = x; queue[0].b = y; map[x][y] = '*'; head = tail = 0; } void push(int x, int y) { queue[tail+1].a = x; queue[tail+1].b = y; map[x][y] = '*'; tail++; } void DFS(int prea, int preb) { int n; int i, ta = prea, tb = preb; initQueue(ta, tb); //printf("%d %d/n", queue[head].a, queue[head].b); count = 1; while(head <= tail) { for(i=0; i<4; i++) { ta = queue[head].a+direct[i][0]; tb = queue[head].b+direct[i][1]; //printf("ta=%d+%d,tb=%d+%d/n", queue[head].a, direct[i][0],queue[head].b, direct[i][1]); //printf("ta=%d tb=%d/n", ta, tb); if(ta>=0 && ta<height && tb>=0 && tb<len) { //printf("%c/n", map[ta][tb]); if(map[ta][tb] == '.') { push(ta, tb); count++; } } //printf("%d %d %d/n", i, queue[head].a, queue[head].b); } //scanf("%d", &n); head++; } printf("%d/n", count); } int main() { int i, j; int pa, pb; while(scanf("%d%d", &len, &height) != EOF) { //printf("%d %d/n", len, height); if(!len && !height)break; for(i=0; i<height; i++) { scanf("%s", map[i]); for(j=0; j<len; j++) { if(map[i][j] == '@') { pa = i; pb = j; } } } //printf("%d %d/n", pa, pb); DFS(pa, pb); } return 1; }
又一道寬搜的題目,練得還可以,深搜的話是不是必須就得遞歸了。