Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

水过,贴代码~~~

Posted by yingxiang720 at 2011-03-22 19:28:11 on Problem 1979
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator