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 <queue> using namespace std; char s[21][21]; int cou; struct pos { int x; int y; int vis; }cb,cs[21][21]; void init() { for(int i = 0;i < 21;i++) for(int j = 0;j < 21;j++) { cs[i][j].x = i; cs[i][j].y = j; cs[i][j].vis = 0; } cou = 0; } int main() { int i,j,w,h; while(scanf("%d%d",&w,&h) != EOF && w != 0 && h != 0) { init(); for(i = 0;i < h;i++) { scanf("%s",s[i]); for(j = 0;j < w;j++) if(s[i][j] == '@') { cb.x = i; cb.y = j; } } queue<pos>q; q.push(cs[cb.x][cb.y]); cs[cb.x][cb.y].vis = 1; cou ++; while(!q.empty()) { pos front = q.front(); int cx = front.x; int cy = front.y; q.pop(); if(!cs[cx - 1][cy].vis && cx - 1 >= 0 && s[cx - 1][cy] == '.') { q.push(cs[cx - 1][cy]); cs[cx - 1][cy].vis = 1; cou++; } if(!cs[cx + 1][cy].vis && cx + 1 < h && s[cx + 1][cy] == '.') { q.push(cs[cx + 1][cy]); cs[cx + 1][cy].vis = 1; cou++; } if(!cs[cx][cy - 1].vis && cy - 1 >= 0 && s[cx][cy - 1] == '.') { q.push(cs[cx][cy - 1]); cs[cx][cy - 1].vis = 1; cou++; } if(!cs[cx][cy + 1].vis && cy + 1 < w && s[cx][cy + 1] == '.') { q.push(cs[cx][cy + 1]); cs[cx][cy + 1].vis = 1; cou++; } } while(!q.empty()) q.pop(); printf("%d\n",cou); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator