听灌佬多 P1126 (玄关)
  • 板块灌水区
  • 楼主Ethan_tang
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/11 17:03
  • 上次更新2024/12/11 20:29:02
查看原帖
听灌佬多 P1126 (玄关)
1491661
Ethan_tang楼主2024/12/11 17:03

原题,全-1

#include <bits/stdc++.h>
using namespace std;
int fx[4] = {0,0,-1,1},fy[4] = {-1,1,0,0},tr[4] = {2,3,1,0},tl[4] = {3,2,0,1},n,m,ans = -1;
bool ma[55][55],vis[55][55][4];
char sface;
struct robot
{
	int face;
	int x;
	int y;
	int t;
};
robot pos(int x,int y,int t,int face)
{
	robot w;
	w.x = x;
	w.y = y;
	w.t = t;
	w.face = face;
	return w;
}
robot st,en;
queue <robot> q;
int main()
{
	cin >> n >> m;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			int w;
			cin >> w;
			if (w)
			{
				ma[i][j] = 1;
				ma[i - 1][j - 1] = 1;
				ma[i - 1][j] = 1;
				ma[i][j - 1] = 1;
			}
		}	
	}
	cin >> st.x >> st.y >> en.x >> en.y;
	cin >> sface;
	st.face = (sface == 'W'?0:(sface == 'E'?1:(sface == 'N'?2:3)));
	q.push(st);
	vis[st.x][st.y][st.face] = 1;
	while (!q.empty())
	{
		int x = q.front().x,y = q.front().y,t = q.front().t,face = q.front().face;
		q.pop();
		if (x = en.x && y == en.y)
		{
			ans = t;
			break ;	
		}
		for (int i = 1;i <= 3;i++)
		{
			int x1 = x + i * fx[face],y1 = y + i * fy[face];
			if (x1 < 1 || y1 < 1 || x1 >= n || y1 >= m || ma[x1][y1])
			{
				break ;	
			}
			if (vis[x1][y1][face])
			{
				continue ;	
			}
			vis[x1][y1][face] = 1;
			q.push(pos(x1,y1,t + 1,face));
		}
		if (!vis[x][y][tr[face]])
		{
			vis[x][y][tr[face]] = 1;
			q.push(pos(x,y,t + 1,tr[face]));
		}
		if (!vis[x][y][tl[face]])
		{
			vis[x][y][tl[face]] = 1;
			q.push(pos(x,y,t + 1,tl[face]));
		}
	}
	cout << ans;
	return 0;
}
2024/12/11 17:03
加载中...