USACO--starry--模拟

模拟题目的恶心真心不是吹的



#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <fstream>

using namespace std;
const int MAXN = 510;
ofstream fout("starry.out");
struct Tri
{
	int x,y;
	int start_x,start_y;
	int end_x,end_y;
};
Tri p[MAXN];
int ic;
char now_c;
char mp[MAXN][MAXN];
int n,m;
void init()
{
	scanf("%d%d",&m,&n);
	for(int i=0;i<n;i++)
	{
		scanf("%s",mp[i]);
	}
}

bool check(int x,int y)
{
	if(x>=n||x<0)return false;
	if(y>=m||y<0)return false;
	return true;
}

bool vis[MAXN][MAXN];
int min_x,min_y;
int max_x,max_y;
void dfs(int x,int y)
{
	vis[x][y]=1;
	min_x=min(min_x,x);
	min_y=min(min_y,y);
	max_x=max(max_x,x);
	max_y=max(max_y,y);
	for(int i=-1;i<2;i++)
	{
		for(int j=-1;j<2;j++)
		{
			int nx = x+i;
			int ny = y+j;
			if(check(nx,ny)&&mp[nx][ny]=='1'&&!vis[nx][ny])
			{
			
				dfs(nx,ny);
			}
		}
	}
}

int pos[MAXN][MAXN];
bool m_vis[MAXN];//mark each square is drawed or not
void cover(int c)
{
	m_vis[c]=1;
	queue<int> q;
	q.push(p[c].x*m+p[c].y);
	mp[p[c].x][p[c].y]=now_c;
	while(!q.empty())
	{
		int u = q.front();q.pop();
		int x = u/m;
		int y = u%m;
		for(int i=-1;i<2;i++)
		{
			for(int j=-1;j<2;j++)
			{
				int nx = x+i;
				int ny = y+j;
				if(check(nx,ny)&&mp[nx][ny]=='1')
				{
					mp[nx][ny] = now_c;
					q.push(nx*m+ny);
				}
			}
		}
	}

}
char mp1[MAXN][MAXN];
char mp2[MAXN][MAXN];
void duichen(char smp[][MAXN],int h,int w)
{
	char temp;
	int num = w/2;
	for(int i=0;i<=h;i++)
	{
		for(int j=0;j<=num;j++)
		{
			temp = smp[i][j];
			smp[i][j] = smp[i][w-j];
			smp[i][w-j]=temp;
		}
	}
}
char mid_mp[MAXN][MAXN];
void fanzhuan180(char smp[][MAXN],int h,int w)
{
	memset(mid_mp,'0',sizeof(mid_mp));

	for(int i=0;i<=h;i++)
		for(int j=0;j<=w;j++)
			mid_mp[i][j] = smp[i][j];

	int x =0,y=0;
	for(int i=h;i>=0;i--)
	{
		y=0;
		for(int j=w;j>=0;j--)
		{
			smp[i][j] = mid_mp[x][y];
			y++;
		}
		x++;
	} 
}
void fanzhuan90(char smp[][MAXN],int &h,int &w)
{
	for(int i=0;i<=h;i++)
	{
		for(int j=0;j<=w;j++)
		{
			mid_mp[w-j][i] = smp[i][j];
		}
	}
	swap(h,w);
	for(int i=0;i<=h;i++)
		for(int j=0;j<=w;j++)
			smp[i][j]=mid_mp[i][j];
}
void op(char p[][MAXN],int h,int w)
{
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
			printf("%c",p[i][j]);
		puts("");
	}
}
#define bug6
bool MyCmp(int h,int w)
{
	for(int i=0;i<=h;i++)
	{
		for(int j=0;j<=w;j++)
		{
			if(mp1[i][j]!=mp2[i][j])
				{
					return false;
				}
		}
	}
	return true;
}


bool judge(int st,int tar)
{
	if(st==tar)return true;

	bool vis[MAXN][MAXN];
	int h=0,w=0;
	int h1=0,w1=0;

	h=p[st].end_x-p[st].start_x;
	w=p[st].end_y-p[st].start_y;

	h1=p[tar].end_x - p[tar].start_x;
	w1=p[tar].end_y - p[tar].start_y;

	if((h!=h1&&w!=w1)&&(h!=w1&&w!=h1))
		{
			return false;
		}

	memset(vis,0,sizeof(vis));
	memset(mp1,'0',sizeof(mp1));
	memset(mp2,'0',sizeof(mp2));

	queue<int> q;

	int u,temp = p[st].x*m+p[st].y;
	q.push(temp);

	vis[p[st].x][p[st].y]=1;
	mp1[p[st].x-p[st].start_x][p[st].y-p[st].start_y] = '1';

	while(!q.empty())
	{
		u = q.front();q.pop();
		int x = u/m;
		int y = u%m;
		for(int i=-1;i<2;i++)
			for(int j=-1;j<2;j++)
			{
				int nx = x+i;
				int ny = y+j;
				if(check(nx,ny)&&mp[nx][ny]=='1'&&!vis[nx][ny])
				{
					mp1[nx-p[st].start_x][ny-p[st].start_y]='1';
					q.push(nx*m+ny);
					vis[nx][ny]=1;
				}
			}
	}
	temp = p[tar].x*m+p[tar].y;
	memset(vis,0,sizeof(vis));
	q.push(temp);
	vis[p[tar].x][p[tar].y]=1;
	mp2[p[tar].x-p[tar].start_x][p[tar].y-p[tar].start_y]='1';

	while(!q.empty())
	{
		u=q.front();q.pop();
		int x = u/m;
		int y = u%m;

		for(int i=-1;i<2;i++)
		for(int j=-1;j<2;j++)
		{
			int nx = x+i;
			int ny = y+j;
			if(check(nx,ny)&&mp[nx][ny]=='1'&&!vis[nx][ny])
			{
				q.push(nx*m+ny);
				vis[nx][ny]=1;
				mp2[nx-p[tar].start_x][ny-p[tar].start_y]='1';
			}
		}
	}
	if(h==h1&&w==w1)
	{
		if(MyCmp(h,w))return true;
		duichen(mp2,h,w);
		if(MyCmp(h,w))return true;
		fanzhuan180(mp2,h,w);
		if(MyCmp(h,w))return true;
		duichen(mp2,h,w);
		if(MyCmp(h,w))return true;
	}
	if(h==w1&&w==h1)
	{
		fanzhuan90(mp2,h1,w1);

		if(MyCmp(h,w))return true;
		duichen(mp2,h,w);
		if(MyCmp(h,w))return true;
		fanzhuan180(mp2,h,w);
		if(MyCmp(h,w))return true;
		duichen(mp2,h,w);
		if(MyCmp(h,w))return true;
	}
	return false;
}
#include <vector>
vector<int> haha;
void draw(int st)
{
	for(int i=0;i<ic;i++)
	{
		if(m_vis[i])continue;
		if(judge(st,i))
		{
			haha.push_back(i);
		}	
	}
	int sz = haha.size();
	for(int i=0;i<sz;i++)
	{
		cover(haha[i]);
	}
	haha.clear();
}
void solve()
{
	memset(vis,0,sizeof(vis));
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='1'&&!vis[i][j])
			{
				min_y=min_x = MAXN;
				max_y=max_x = -MAXN;
				dfs(i,j);
				p[ic].start_x=min_x;
				p[ic].start_y=min_y;
				p[ic].end_x=max_x;
				p[ic].end_y=max_y;
				p[ic].x=i;
				p[ic].y=j;
				pos[i][j] = ic++;
			}
		}
	}	
	now_c = 'a';
	memset(m_vis,0,sizeof(m_vis));
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='1')
			{
				draw(pos[i][j]);
				now_c++;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			printf("%c",mp[i][j]);
		}
		puts("");
	}
}

int main()
{
	freopen("starry.in","r",stdin);
	freopen("starry.out","w",stdout);
	init();
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>