样例都没过qwq求助
  • 板块P2802 回家
  • 楼主Ice_rnfmabj
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/11/19 20:30
  • 上次更新2023/11/19 23:12:32
查看原帖
样例都没过qwq求助
695943
Ice_rnfmabj楼主2023/11/19 20:30

RT

#include<bits/stdc++.h>
using namespace std;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
const int INF=0x6f;
int n,m,Map[10][10],sx,sy,ans=INF;
int a[10][10][10];
void dfs(int x,int y,int step,int tili){
	if(Map[x][y]==3){
		ans=step;
		return;
	}
	if(Map[x][y]==4) tili=6;
	if(tili<=1||step>ans||Map[x][y]==0||step>=a[x][y][tili]) return;
	a[x][y][tili]=step;
	for(int i=1;i<=4;i++){
		int tx=x+dx[i],ty=y+dy[i];
		dfs(tx,ty,step+1,tili-1);
	}
	return;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			scanf("%d",&Map[i][j]);
			if(Map[i][j]==2){
				sx=i;sy=j;
			}
		}
	}
	dfs(sx,sy,0,6);
	if(ans==INF) cout<<-1;
	else printf("%d\n",ans);
	return 0;
}
2023/11/19 20:30
加载中...