#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <queue>
#define INPUT
using 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;
}
【poj1164】The Castle,解題報告+思路+代碼+數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.