zoukankan      html  css  js  c++  java
  • [洛谷] P1126 机器人搬重物(BFS搜图好题) Apare_xzc

    [洛谷] P1126 机器人搬重物(BFS搜图好题)


    题目链接

    在这里插入图片描述

    /*
    洛谷P1126 机器人搬重物
    Apare_xzc 2019.12.8
    编程语言   代码长度    用时    内存
    C++        3.82KB      34ms    784.00KB 
    */
    #include <bits/stdc++.h>
    using namespace std;
    int maze[100][100],sx,sy,ex,ey,m,n,sdir;
    bool vis[100][100][4];
    char ddd[3];
    struct Node{ //结构体用于存储bfs时队列中存储的状态(坐标,方向,时间) 
    	int x,y,step,dir;//横坐标,总坐标,用时,方向 
    	Node(int _x=0,int _y=0,int _step=0,int _dir=0):x(_x),y(_y),step(_step),dir(_dir){}
    }node;
    bool ok(int x,int y,int dir);//可以走 
    bool out(int x,int y);//出界了 
    pair<int,int> move(int x,int y,int dir,int len);//返回停下的位置 
    bool checkCreep(int xx,int yy,int dir,int &nx,int &ny);//走一步
    bool checkWalk(int xx,int yy,int dir,int& nx,int& ny); //走两步
    bool checkRun(int xx,int yy,int dir,int& nx,int &ny); //走三步 
    inline int toRight(int dir){return (dir+1)%4;} //右转 
    inline int toLeft(int dir){return (dir+3)%4;}  //左转 
    void bfs();
     
    int main()
    {
    	int ca = 0;
    	while(cin>>n>>m)
    	{
    		if(ca++) memset(vis,0,sizeof(vis));
    		for(int i=1; i<=n; ++i)
    			for(int j=1;j<=m;++j)
    				scanf("%d",&maze[i][j]);
    		scanf("%d%d%d%d%s",&sx,&sy,&ex,&ey,ddd);
    		char sss[] = "ESWN666";
    		for(int i=0;i<4;++i) //确定开始的方向 ESWN对应0123 
    			if(sss[i]==ddd[0]) sdir = i;
    		bfs();
    	}
    	
    	return 0;
    }
    bool ok(int x,int y,int dir)
    {
    	return (x>0&&x<=n&&y>0&&y<=m&&!vis[x][y][dir]&&maze[x][y]==0);
    }
    bool out(int x,int y)
    {
    	return (x<=0||x>n||y<=0||y>m);
    } 
    pair<int,int> move(int x,int y,int dir,int len)
    {
    	switch (dir)
    	{
    		case 0: return make_pair(x,y+len);
    		case 1: return make_pair(x+len,y);
    		case 2: return make_pair(x,y-len);
    		case 3: return make_pair(x-len,y);	
    	}
    	return make_pair(0,0);
    }
    
    bool checkCreep(int xx,int yy,int dir,int &nx,int &ny)
    {
    	pair<int,int> pr = move(xx,yy,dir,1);
    	int x = pr.first, y = pr.second;
    	nx = x, ny = y;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	return !vis[x][y][dir];
    }
    bool checkWalk(int xx,int yy,int dir,int& nx,int& ny)
    {
    	pair<int,int> pr = move(xx,yy,dir,1);
    	int x = pr.first, y = pr.second;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	pr = move(xx,yy,dir,2);
    	x = pr.first, y = pr.second;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	nx = x, ny = y;
    	return !vis[x][y][dir];
    }
    bool checkRun(int xx,int yy,int dir,int& nx,int &ny)
    {
    	pair<int,int> pr = move(xx,yy,dir,1);
    	int x = pr.first, y = pr.second;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	pr = move(xx,yy,dir,2);
    	x = pr.first, y = pr.second;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	pr = move(xx,yy,dir,3);
    	x = pr.first, y = pr.second;
    	if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;  
    	if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
    	nx = x, ny = y;
    	return !vis[x][y][dir];
    }
    void bfs()
    {
    	queue<Node> Q;
    	Q.push(Node(sx,sy,0,sdir));
    	vis[sx][sy][sdir] = true;
    	int ans = -1;
    	while(!Q.empty())
    	{
    		node = Q.front();
    		Q.pop();
    		int x = node.x, y = node.y;
    		int step = node.step, dir = node.dir;
    		if(x==ex&&y==ey)
    		{
    			ans = step;
    			break;
    		}
    		int nx=0,ny=0;
    		if(checkRun(x,y,dir,nx,ny))
    		{
    			vis[nx][ny][dir] = true;
    			Q.push(Node(nx,ny,step+1,dir));
    		}
    		if(checkWalk(x,y,dir,nx,ny))
    		{
    			vis[nx][ny][dir] = true;
    			Q.push(Node(nx,ny,step+1,dir));
    		}
    		if(checkCreep(x,y,dir,nx,ny))
    		{
    			vis[nx][ny][dir] = true;
    			Q.push(Node(nx,ny,step+1,dir));
    		}
    		if(!vis[x][y][toLeft(dir)])
    		{
    			vis[x][y][toLeft(dir)] = true;
    			Q.push(Node(x,y,step+1,toLeft(dir)));	
    		}	
    		if(!vis[x][y][toRight(dir)])
    		{
    			vis[x][y][toRight(dir)] = true;
    			Q.push(Node(x,y,step+1,toRight(dir)));
    		}
    	}
    	printf("%d
    ",ans);
    }
    

    在这里插入图片描述


  • 相关阅读:
    The Worm Turns
    Equations
    Snail’s trouble
    WuKong
    Codeforces 369 C Valera and Elections
    POJ 2186 Popular Cows
    Codefroces 366 D Dima and Trap Graph (最短路)
    Codefroces 366 C Dima and Salad(dp)
    Codefroces 374 B Inna and Sequence (树状数组 || 线段树)
    Codeforces 374 C Inna and Dima (DFS)
  • 原文地址:https://www.cnblogs.com/Apare-xzc/p/12243625.html
Copyright © 2011-2022 走看看