关于方向的疑问
  • 板块P1238 走迷宫
  • 楼主shicj
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/9/5 21:38
  • 上次更新2023/11/2 22:37:03
查看原帖
关于方向的疑问
678625
shicj楼主2023/9/5 21:38
#include<iostream>
#include<vector>
using namespace std;
int n,m,sx,sy,ex,ey;
//int dx[4]={-1,0,1,0};
//int dy[4]={0,-1,0,1};
//
//int dx[4]={-1,0,1,0};
//int dy[4]={0,1,0,-1};
//
//int dx[4]={1,0,-1,0};
//int dy[4]={0,-1,0,1};
//
//int dx[4]={1,0,-1,0};
//int dy[4]={0,1,0,-1};
//
//int dx[4]={0,1,0,-1};
//int dy[4]={1,0,-1,0};
//
//int dx[4]={0,1,0,-1};
//int dy[4]={-1,0,1,0};
//
//int dx[4]={0,-1,0,1};
//int dy[4]={1,0,-1,0};
//
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
//上左下右
struct xy{
	int x,y;
};
struct node{
	vector<xy>pts;
};
bool vis[18][18];
int mp[18][18];
int sum;
void print(node no){
	sum++;
	cout<<"("<<no.pts[0].x<<","<<no.pts[0].y<<")";
	for(int i=1;i<no.pts.size();i++)
		cout<<"->("<<no.pts[i].x<<","<<no.pts[i].y<<")";
	cout<<endl;
}
void dfs(node now){
	int nx=now.pts[now.pts.size()-1].x;
	int ny=now.pts[now.pts.size()-1].y;
	for(int i=0;i<4;i++){
		int xx=nx+dx[i];
		int yy=ny+dy[i];
		if(mp[xx][yy]&&!vis[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m){
//			cout<<xx<<" "<<yy<<endl;
			if(xx==ex&&yy==ey){
				node tmp=now;
				tmp.pts.push_back({xx,yy});
				print(tmp);
			}
			else{
				vis[xx][yy]=true;
				node tmp=now;
				tmp.pts.push_back({xx,yy});
				dfs(tmp);
				vis[xx][yy]=false;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	cin>>sx>>sy;
	cin>>ex>>ey;
	node st;
	st.pts.clear();
	st.pts.push_back({sx,sy});
	vis[sx][sy]=true;
	dfs(st);
	if(sum==0){
		cout<<"-1"<<endl;
	}
	return 0;
}

明明题目说左上右下,为什么我上左下右才AC(一种一种试出来的)?

2023/9/5 21:38
加载中...