P6207 [USACO06OCT] Cows on Skates G做题笔记

开始以为能秒切,结果发现不清楚如何实现路径输出,于是乎瞅了眼题解,发现是递归输出路径,\(AC\) 后搞不清为啥要先输出 1 1,因为如果这样我的代码会输出两次 1 1,后来发现我的输出方式是递归到 0 0 再返回,而题解递归到 1 1 就返回了,没有输出 1 1,所以我的代码会多输出一次 1 1

#include <bits/stdc++.h>
  using namespace std;
int n,m,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},u,v,i,j;
char c[150][100];
struct xy{
	int x,y;
}t,s[150][100];
queue<xy>q;
void bfs()
{
	c[1][1]='+';
	q.push(xy{1,1});
	while (!q.empty())
	{
		t=q.front();
		q.pop();
		for (i=0;i<4;i++)
		{
			u=t.x+dx[i];
			v=t.y+dy[i];
			if (u>=1 && u<=n && v>=1 && v<=m && c[u][v]=='.')
			{
				c[u][v]='+';
				s[u][v].x=t.x;
				s[u][v].y=t.y;
				if (u==n && v==m) return;
				q.push(xy{u,v});
			}
		}
	}
}
void write(int p,int q)
{
	if (!p && !q) return;
	write(s[p][q].x,s[p][q].y);
	cout<<p<<" "<<q<<endl;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(nullptr);
	cin>>n>>m;
	for (i=1;i<=n;i++)
	{
		for (j=1;j<=m;j++)
		{
			cin>>c[i][j];
		}
	}
	bfs();
	write(n,m);
	return 0;
}
posted @ 2022-06-06 15:52  Jason142  阅读(109)  评论(0编辑  收藏  举报