Java P1126 机器人搬重物

63 篇文章 1 订阅

题目链接
做了一下午终于做出这道题了,真是恶心。
下面是Ac代码

import java.util.*;
public class Main {
	public static int n,m,zhangAi[][],sx,sy,ex,ey;
	public static int robot[][][],dx[]={0,1,0,-1},dy[]={1,0,-1,0},is[][][];
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		m=sc.nextInt();
		zhangAi=new int[n][m];
		robot=new int[n+1][m+1][2];  //0 is   1  dir
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++)
				zhangAi[i][j]=sc.nextInt();
		}
		sx=sc.nextInt();
		sy=sc.nextInt();
		ex=sc.nextInt();
		ey=sc.nextInt();
		robot[sx][sy][0]=1;
		char dir=sc.next().charAt(0);
		if(dir=='E'){//you
			robot[sx][sy][1]=0;
		}
		else if(dir=='S'){   //xia
			robot[sx][sy][1]=1;
		}
		else if(dir=='W'){  //zuo
			robot[sx][sy][1]=2;
		}
		else if(dir=='N'){     //shang
			robot[sx][sy][1]=3;
		}
		System.out.println(bfs());
		
	}
	public static int bfs(){
		Queue<int[]> q=new LinkedList<int[]>();
		is=new int[n+1][m+1][4];
		q.offer(new int[]{sx,sy,robot[sx][sy][1],0});
		is[sx][sy][robot[sx][sy][1]]=1;
		while(!q.isEmpty()){
			int[] cur=q.poll();
			int x=cur[0];
			int y=cur[1];
			int curdir=cur[2];
			int curstep=cur[3];
			if(x==ex&&y==ey)
				return curstep;
			if(is[x][y][(curdir+1+4)%4]==0){
				is[x][y][(curdir+1+4)%4]=1;
				q.offer(new int[]{x,y,(curdir+1+4)%4,curstep+1});
			}
			if(is[x][y][(curdir-1+4)%4]==0){
				is[x][y][(curdir-1+4)%4]=1;
				q.offer(new int[]{x,y,(curdir-1+4)%4,curstep+1});
			}
			for(int i=1;i<=3;i++){
				int xx=x+dx[curdir]*i;
				int yy=y+dy[curdir]*i;
				if(xx<=0||yy<=0||xx>=n||yy>=m||!curdian(xx,yy)||predian(xx-dx[curdir],yy-dy[curdir])||is[xx][yy][curdir]==1)
				continue;
				q.offer(new int[]{xx,yy,curdir,curstep+1});
				is[xx][yy][curdir]=1;
			}
		}
		return -1;
	}
	public static boolean curdian(int x,int y){
		return (zhangAi[x-1][y-1]!=1&&zhangAi[x][y-1]!=1&&zhangAi[x-1][y]!=1&&zhangAi[x][y]!=1);
	}
	public static boolean predian(int x,int y){    //zuoguo false
		for(int i=0;i<4;i++){
			if(is[x][y][i]!=0)
				return false;
		}
		return true;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔崔谁用的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值