Hack 过了但是 60 球调
查看原帖
Hack 过了但是 60 球调
827018
zhouzihang1楼主2024/5/23 21:20
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int,int> PII;
const int N=2e2+10,C=20+10;
int n,m,k,c;
char ch[N][N];
//ch 存地图
struct snake{
	int len,id;
	deque<PII > dq;
  //front 是蛇头,back 是蛇尾
	bool died;
  //0死1活
	void init()
	{
		len=id=died=0;
		while(dq.size()) dq.pop_back();
	}
}sk[C];
bool cmp1(snake a_,snake b_)
{
	if(a_.dq[0].first==b_.dq[0].first) return a_.dq[0].second<b_.dq[0].second;
	return a_.dq[0].first<b_.dq[0].first;
}
//编号排序
bool cmp2(snake a_,snake b_)
{
	if(a_.len==b_.len) return a_.id<b_.id;
	return a_.len>b_.len;
}
bool vis[N][N];
//这里有没有蛇
void bfs(int stx,int sty)
{
	queue<PII > q;
	while(q.size()) q.pop();
	++c;
	int dx[]={0,0,-1,1};
	int dy[]={-1,1,0,0};
	vis[stx][sty]=1;
	q.push({stx,sty});
	while(q.size())
	{
		int x=q.front().first;
		int y=q.front().second;
		q.pop();
		sk[c].dq.push_back({x,y});
		sk[c].len++;
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i];
			int yy=y+dy[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!vis[xx][yy]&&ch[xx][yy]=='#')
			{
				q.push({xx,yy});
				vis[xx][yy]=1;
			}
		}
	}
}
void Clear(int i)//i 号蛇死了
{
	sk[i].died=1;
	sk[i].len=0;
	for(PII it:sk[i].dq)
	{
		int x=it.first,y=it.second;
		vis[x][y]=0;
		ch[x][y]='&';
	}
}
void work(int i,char op)
{
	if(sk[i].died) return;
	int x=sk[i].dq[0].first;
	int y=sk[i].dq[0].second;
//	cerr<<x<<' '<<y<<'\n';
	ch[x][y]='#';
	if(op=='W') x--;
	if(op=='D') y++;
	if(op=='S') x++;
	if(op=='A') y--;
//	cerr<<x<<' '<<y<<'\n';
	if(x<1||x>n||y<1||y>m||vis[x][y]||ch[x][y]=='#')
	{
		Clear(i);
		return;
	}
	if(ch[x][y]=='.')
	{
		ch[x][y]='@';
		vis[x][y]=1;
		ch[sk[i].dq.back().first][sk[i].dq.back().second]='.';
		vis[sk[i].dq.back().first][sk[i].dq.back().second]=0;
		sk[i].dq.pop_back();
		sk[i].dq.push_front({x,y});
	}
	if(ch[x][y]=='&')
	{
		ch[x][y]='@';
		sk[i].len++;
		vis[x][y]=1;
		sk[i].dq.push_front({x,y});
	}
}
int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>ch[i][j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(ch[i][j]=='@') bfs(i,j);
	sort(sk+1,sk+c+1,cmp1);
	for(int i=1;i<=c;i++) sk[i].id=i;
//	for(int i=1;i<=c;i++)
//	{
//		cout<<"-----   "<<sk[i].id<<' '<<sk[i].len<<'\n';
//		for(PII it:sk[i].dq) printf("%d %d\n",it.first,it.second);
//	}
	for(int i=1;i<=c;i++)
	{
		char op;
		for(int j=1;j<=k;j++)
		{
			cin>>op;
			work(i,op);
		}
	}
	sort(sk+1,sk+c+1,cmp2);
	for(int i=1;i<=c;i++)
		printf("%d %d\n",sk[i].len,sk[i].id);
	int res=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			res+=(ch[i][j]=='&');
	printf("%d",res);
	return 0;
}
/*
讨论区的 Hack
5 7 6 
....... 
....@.. 
....... 
.......  
....... 
DSAWDS 

1 1
0
*/
2024/5/23 21:20
加载中...