这个题目费了我好长时间,虽然不难,但是收获不小。贴出来看看
#include <fstream>
#include <iostream>#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 55;
int n,m;
int map[maxn][maxn];
int mark[maxn][maxn];
bool visit[maxn][maxn];
int tongji[maxn*maxn];
int cnt;
int find(int x,int y)
{
int ret = 1;
visit[x][y] = true;
queue< pair<int,int> > Q;
Q.push( make_pair(x,y) );
while( !Q.empty() )
{
pair<int,int> T = Q.front();
int tx = T.first;
int ty = T.second;
mark[tx][ty] = cnt;
Q.pop();
if( ((map[tx][ty]&1)==0) && ty != 1 && !visit[tx][ty-1]) { Q.push( make_pair(tx,ty-1)); ret++; visit[tx][ty-1] = true;}
if( ((map[tx][ty]&2)==0) && tx != 1 && !visit[tx-1][ty]) { Q.push( make_pair(tx-1,ty)); ret++; visit[tx-1][ty] = true;}
if( ((map[tx][ty]&4)==0) && ty != m && !visit[tx][ty+1]) { Q.push( make_pair(tx,ty+1)); ret++; visit[tx][ty+1] = true;}
if( ((map[tx][ty]&8)==0) && tx != n && !visit[tx+1][ty]) { Q.push( make_pair(tx+1,ty)); ret++; visit[tx+1][ty] = true;}
}
tongji[cnt] = ret;
return ret;
}
int main()
{
ifstream fin("castle.in");
fin >> m >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
fin >> map[i][j];
}
}
memset(visit,0,sizeof(visit));
memset(tongji,0,sizeof(tongji));
memset(mark,0,sizeof(mark));
int max = 0;
cnt = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(!visit[i][j])
{
cnt++;
int ret = find(i,j);
if(ret > max)
{
max = ret;
}
}
}
}
int pmax = 0;
int x,y;
char dir;
for(int j = 1; j <= m; j++)
{
for(int i = n; i >= 1; i--)
{
int tmp = 0;
if( (map[i][j]&2)!=0 && i != 1)
{
tmp += tongji[mark[i][j]];
if( mark[i][j] != mark[i-1][j]) tmp += tongji[mark[i-1][j]];
if( tmp > pmax)
{
pmax = tmp;
dir = 'N';
x = i;
y = j;
}
}
tmp = 0;
if( (map[i][j]&4)!=0 && j != m)
{
tmp += tongji[mark[i][j]];
if(mark[i][j] != mark[i][j+1]) tmp += tongji[mark[i][j+1]];
if( tmp > pmax)
{
pmax = tmp;
dir = 'E';
x = i;
y = j;
}
}
}
}
ofstream fout("castle.out");
fout << cnt << "\n" << max << endl;
fout << pmax << "\n" << x << " " << y << " " << dir << endl;
return 0;
}