【poj1164】The Castle,解题报告+思路+代码+数据

来源:互联网 发布:知福惜福下一句是什么 编辑:程序博客网 时间:2024/06/05 17:36
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <stack>#include <queue>#define INPUTusing namespace std;/**    Problem: POJ1164    Begin Time:7th/3/2012 10.00 p.m.    End Time:2012-03-08 01:28:21    Test Data: programming.grids.cn,有,搜索“城堡”    Standard input:    思路:    题目说城堡被分成了不同的Moudle,那么我们就读入每个moudle的信息,然后按照给的    moudle的信息来进行查找,注意每个moudle之间是有影响的,所以构造测试数据的时候需要注意这点。    我们需要有一个标记数组,把访问过得全标记成1,visited[i][j] = 1,在读入数据的时候,我们要记录所有的    moudle,并放入begin这个队列中,    1.从begin.front(),取出元素,numroom++(房间的个数++)    2.以这点开始DFS,每次达到一个visited[i][j]!=1的节点,就把nowsize++    3.比较nowsize与maxsize的大小,if nowsize > maxsize then maxsize = nowsize    3.从begin.front()取出下一个元素,如果begin.empty(),出结果就可以了    教训:    搜索的时候注意条件,此题WA的原因就是因为在nowsize++的时候    没有判断visited[tmp.x][tmp.y]是否为1!因为搜索的方向可能是绕了一圈回来的。    (工,口,工的数据,我的程序就是沿着外面绕了一圈!)    挂掉的数据是    工 口 工 的这么一个数据。    我估计这跟搜索的顺序也有关系,我发现我的搜索顺序是上下左右。*/const int c0de4fun = 60;int movex[4] = {1,-1,0,0};int movey[4] = {0,0,-1,1};int maze[c0de4fun][c0de4fun];int visited[c0de4fun][c0de4fun];int maxroom = 0;struct node{    int x;    int y;};queue<node> begins;void Solve(int H,int W){    stack<node> nodes;    node tmp;    node tmp1;    int roomnum = 0;    int nowsize = 0;    while(!begins.empty())    {        ///初始节点不为空        nowsize = 0;        tmp = begins.front();begins.pop();        if(visited[tmp.x][tmp.y] != 1)        {            nodes.push(tmp);            roomnum++;        }        while(!nodes.empty())        {  ///nodes不空            tmp = nodes.top();nodes.pop();            if(visited[tmp.x][tmp.y] != 1)            {                nowsize++;            }            visited[tmp.x][tmp.y] = 1;        for(int i = 0 ; i < 4; i++)        {            tmp1.x = tmp.x + movex[i];            tmp1.y = tmp.y + movey[i];            /////////条件判断            if ( i == 0 ) //南            {                if( maze[tmp.x][tmp.y] == 8 ||                   maze[tmp.x][tmp.y] == 9 ||                   maze[tmp.x][tmp.y] == 10 ||                   maze[tmp.x][tmp.y] == 11 ||                   maze[tmp.x][tmp.y] == 12 ||                   maze[tmp.x][tmp.y] == 13 ||                   maze[tmp.x][tmp.y] == 14 ||                   maze[tmp.x][tmp.y] == 15 ||                   tmp1.x > H || tmp1.y > W ||                   tmp1.x < 1 || tmp1.y < 1 ||                   visited[tmp1.x][tmp1.y] == 1)                   continue;  //这些组合不能往南            }            if ( i == 1 ) //北            {                if( maze[tmp.x][tmp.y] == 2 ||                   maze[tmp.x][tmp.y] == 3 ||                   maze[tmp.x][tmp.y] == 6 ||                   maze[tmp.x][tmp.y] == 7 ||                   maze[tmp.x][tmp.y] == 10 ||                   maze[tmp.x][tmp.y] == 11 ||                   maze[tmp.x][tmp.y] == 14 ||                   maze[tmp.x][tmp.y] == 15 ||                   tmp1.x > H || tmp1.y > W ||                   tmp1.x < 1 || tmp1.y < 1 ||                   visited[tmp1.x][tmp1.y] == 1)                   continue;  //这些组合不能往北            }            if ( i == 2 ) //西            {                if( maze[tmp.x][tmp.y] == 1 ||                   maze[tmp.x][tmp.y] == 3 ||                   maze[tmp.x][tmp.y] == 5 ||                   maze[tmp.x][tmp.y] == 7 ||                   maze[tmp.x][tmp.y] == 9 ||                   maze[tmp.x][tmp.y] == 11 ||                   maze[tmp.x][tmp.y] == 13 ||                   maze[tmp.x][tmp.y] == 15 ||                   tmp1.x > H || tmp1.y > W ||                   tmp1.x < 1 || tmp1.y < 1 ||                   visited[tmp1.x][tmp1.y] == 1)                   continue;  //这些组合不能往西            }            if ( i == 3 ) // 东            {                if( maze[tmp.x][tmp.y] == 4 ||                   maze[tmp.x][tmp.y] == 5 ||                   maze[tmp.x][tmp.y] == 6 ||                   maze[tmp.x][tmp.y] == 7 ||                   maze[tmp.x][tmp.y] == 12 ||                   maze[tmp.x][tmp.y] == 14 ||                   maze[tmp.x][tmp.y] == 13 ||                   maze[tmp.x][tmp.y] == 15 ||                   tmp1.x < 1 || tmp1.y < 1 ||                   tmp1.x > H || tmp1.y > W ||                   visited[tmp1.x][tmp1.y] == 1)                   continue;  //这些组合不能往东            }            nodes.push(tmp1);        }        }        if( nowsize > maxroom)        {            maxroom = nowsize;        }    }    while(!nodes.empty())        nodes.pop();    printf("%d\n",roomnum);    printf("%d\n",maxroom);}int main(){    int H,W,startx,starty;    node tmp;#ifdef  INPUT    freopen("b:\\acm\\poj1164\\input.txt","r",stdin);    freopen("b:\\acm\\poj1164\\output.txt","w",stdout);#endif  //  while(scanf("%d%d",&H,&W) != EOF)        scanf("%d%d",&H,&W);    {        maxroom = 0;        for(int i = 1 ; i <= H; i++)        {            for(int j = 1 ; j <= W; j++)            {                scanf("%d",&maze[i][j]);                tmp.x = i;tmp.y = j;                begins.push(tmp);            }        }        Solve(H,W);    }#ifdef INPUT    fclose(stdin);    fclose(stdout);#endif    return 0;}