您的位置:首页 > 其它

USACO 2.1 The Castle

2011-05-03 11:20 337 查看
/*
ID: jiafeim1
PROG: castle
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

#define maxN(x,y) ((x)>(y)?(x):(y))
#include <queue>

int wall[53][53];
int whatSet[53][53];

int top = 0;
int squre[2503];
int max_squre = 0;
struct point
{
int x;
int y;
point(int xi,int yi)
{
x=xi;
y=yi;
}
};
void bfs(int x,int y)
{
int s = 0;
queue<point> wq;
whatSet[x][y] = top;
wq.push(point(x,y));
while(!wq.empty())
{
++s;
point p = wq.front();
wq.pop();
int row = p.x;
int column = p.y;
int wal = wall[row][column];
if(!(wal&1) && whatSet[row][column-1] == -1)
{
wq.push(point(row,column-1));
whatSet[row][column-1] = top;
}
if(!(wal&2) && whatSet[row-1][column] == -1)
{
wq.push(point(row-1,column));
whatSet[row-1][column] = top;
}
if(!(wal&4) && whatSet[row][column+1] == -1)
{
wq.push(point(row,column+1));
whatSet[row][column+1] = top;
}
if(!(wal&8) && whatSet[row+1][column] == -1)
{
wq.push(point(row+1,column));
whatSet[row+1][column] = top;
}
}
squre[top] = s;
if(s>max_squre) max_squre = s;
++top;
}

char ward;
int wall_row;
int wall_column;
int destory_max = 0;

int main()
{
ofstream fout ("castle.out");
ifstream fin ("castle.in");

int m,n;
fin>>m>>n;

for(int i = 0 ;i!=n;++i)
for(int j = 0;j!=m;++j)
{
fin>>wall[i][j];
whatSet[i][j] = -1;
}
for(int i = 0;i!=n;++i)
for(int j= 0;j!=m;++j)
{
if(whatSet[i][j] == -1)
bfs(i,j);
}

for(int column = 0 ;column !=m;++column)
for(int row = n-1;row >=0;--row)
{
int wal = wall[row][column];
if(row>=1&&(wal&2)&&(whatSet[row][column]!=whatSet[row-1][column]))
{
if(squre[whatSet[row][column]]+squre[whatSet[row-1][column]]>destory_max)
{
destory_max = squre[whatSet[row][column]]+squre[whatSet[row-1][column]];
wall_row = row + 1;
wall_column = column + 1;
ward = 'N';
}
}
if(column<m-1&&(wal&4)&&(whatSet[row][column]!=whatSet[row][column+1]))
{
if(squre[whatSet[row][column]]+squre[whatSet[row][column+1]]>destory_max)
{
destory_max = squre[whatSet[row][column]]+squre[whatSet[row][column+1]];
wall_row = row + 1;
wall_column = column + 1;
ward = 'E';
}
}
}
fout<<top<<endl;
fout<<max_squre<<endl;
fout<<destory_max<<endl;
fout<<wall_row<<" "<<wall_column<<" "<<ward<<endl;
fin.close();
fout.close();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: