• POJ3322 Bloxorz I 题解


    前言

    20min内单独现写AC此题,DFS、BFS基本过关了。

    题目

    链接

    http://poj.org/problem?id=3322

    字面描述

    Bloxorz I
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 9411 Accepted: 2936
    Description

    Little Tom loves playing games. One day he downloads a little computer game called ‘Bloxorz’ which makes him excited. It’s a game about rolling a box to a specific position on a special plane. Precisely, the plane, which is composed of several unit cells, is a rectangle shaped area. And the box, consisting of two perfectly aligned unit cube, may either lies down and occupies two neighbouring cells or stands up and occupies one single cell. One may move the box by picking one of the four edges of the box on the ground and rolling the box 90 degrees around that edge, which is counted as one move. There are three kinds of cells, rigid cells, easily broken cells and empty cells. A rigid cell can support full weight of the box, so it can be either one of the two cells that the box lies on or the cell that the box fully stands on. A easily broken cells can only support half the weight of the box, so it cannot be the only cell that the box stands on. An empty cell cannot support anything, so there cannot be any part of the box on that cell. The target of the game is to roll the box standing onto the only target cell on the plane with minimum moves.

    The box stands on a single cell

    The box lies on two neighbouring cells, horizontally

    The box lies on two neighbouring cells, vertically
    After Little Tom passes several stages of the game, he finds it much harder than he expected. So he turns to your help.

    Input

    Input contains multiple test cases. Each test case is one single stage of the game. It starts with two integers R and C(3 ≤ R, C ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which contains R lines and C characters for each line, with ‘O’ (Oh) for target cell, ‘X’ for initial position of the box, ‘.’ for a rigid cell, ‘#’ for a empty cell and ‘E’ for a easily broken cell. A test cases starts with two zeros ends the input.

    It guarantees that

    There’s only one ‘O’ in a plane.
    There’s either one ‘X’ or neighbouring two 'X’s in a plane.
    The first(and last) row(and column) must be ‘#’(empty cell).
    Cells covered by ‘O’ and ‘X’ are all rigid cells.
    Output

    For each test cases output one line with the minimum number of moves or “Impossible” (without quote) when there’s no way to achieve the target cell.

    Sample Input

    7 7
    #######
    #…X###
    #…##O#
    #…E#
    #…E#
    #…#
    #######
    0 0
    Sample Output

    10
    Source

    POJ Monthly–2007.08.05, Rainer

    思路

    此题最大的问题就是存图,又2个点的情况,又有1个点的情况如何存储状态呢?

    2个点的坐标一起存,耗费的空间为O((nm)^2),MLE了;

    但2个点的坐标一定相邻,所以存一个核心点的坐标,还有一个所属状态,状态分三种:单个点,竖躺,横躺。竖躺核心点统一记录上面的点,横躺同一记录左面的点。开始写吧

    代码实现

    #include
    #include
    using namespace std;
    
    // 0 单点  1 竖躺 2 横躺 
    const int maxn=500+10;
    const int inf=1e9;
    //  上 下 左 右 
    //状态为i,像j号方向转动,核心点坐标及状态的变化
    const int dx[3][4]=  {{-2,1, 0,0},{-1,2, 0,0},{-1,1, 0,0}};
    const int dy[3][4]=  {{ 0,0,-2,1},{ 0,0,-1,1},{ 0,0,-1,2}};
    const int dlie[3][4]={{ 1,1, 2,2},{ 0,0, 1,1},{ 2,2, 0,0}};
    int n,m;
    char a[maxn][maxn];
    int dis[maxn][maxn][5];
    struct vertex{
    	int x,y,lie;
    };
    vertex st,ed;
    queue<vertex>q;
    //初始化
    inline void init(){
    	st.x=st.y=st.lie=ed.x=ed.y=ed.lie=0;
    	while(!q.empty())q.pop();
    	for(int i=1;i<=n+1;i++){
    		for(int j=1;j<=m+1;j++){
    			for(int k=0;k<3;k++)dis[i][j][k]=inf;
    		}
    	}
    }
    //是否在界内
    inline bool inbound(int nx,int ny){return nx>=1&&nx<=n&&ny>=1&&ny<=m;}
    //是否合法
    inline bool in_law(vertex op){
    	if(!inbound(op.x,op.y))return false;
    	if(a[op.x][op.y]=='#')return false;
    	if(op.lie==1&&a[op.x+1][op.y]=='#')return false;
    	if(op.lie==2&&a[op.x][op.y+1]=='#')return false;
    	if(op.lie==0&&a[op.x][op.y]=='E')return false;
    	return true;
    }
    //bfs
    inline int bfs(){
    	dis[st.x][st.y][st.lie]=0;
    	q.push(st);
    	while(!q.empty()){
    		vertex point=q.front();
    		//printf("%d %d %d\n",point.x,point.y,point.lie);
    		q.pop();
    		for(int i=0;i<4;i++){
    			vertex nxt;
    			nxt.x=point.x+dx[point.lie][i];
    			nxt.y=point.y+dy[point.lie][i];
    			nxt.lie=dlie[point.lie][i];
    			if(!in_law(nxt))continue;
    			if(dis[nxt.x][nxt.y][nxt.lie]==inf){
    				dis[nxt.x][nxt.y][nxt.lie]=dis[point.x][point.y][point.lie]+1;
    				q.push(nxt);
    				//printf("%d %d %d\n",nxt.x,nxt.y,nxt.lie);
    				if(nxt.x==ed.x&&nxt.y==ed.y&&nxt.lie==ed.lie)return dis[ed.x][ed.y][ed.lie];
    			}
    		}
    	}
    	return inf;
    }
    int main(){
    	while(1){
    		scanf("%d%d",&n,&m);
    		if(n==0&&m==0)break;
    		init();
    		//求起始及终点坐标及状态
    		for(int i=1;i<=n;i++){
    			for(int j=1;j<=m;j++){
    				scanf(" %c",&a[i][j]);
    				if(a[i][j]=='X'){
    					if(st.x!=0&&st.y!=0){
    						if(j-1==st.y)st.lie=2; 
    						else if(i-1==st.x)st.lie=1;
    					}
    					else st.x=i,st.y=j;
    				}
    				else if(a[i][j]=='O')ed.x=i,ed.y=j;
    			}
    		}
    		//printf("%d %d %d\n",ed.x,ed.y,ed.lie);
    		int ans=bfs();
    		if(ans==inf)printf("Impossible\n");
    		else printf("%d\n",ans);
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
  • 相关阅读:
    chatgpt赋能python:Python怎么入侵手机
    C# 中的那些锁,在内核态都是怎么保证同步的?
    笔记本重装系统找回预装的office
    【译】Based:简单线性注意力语言模型平衡召回-吞吐量权衡
    Java 集合之 Queue 和 Deque
    Keras深度学习实战(16)——自编码器详解
    内核驱动mmap Handler利用技术(二)
    全球首个“AI程序员”Deven诞生,真的能替代人类程序员吗?
    基于RabbitMQ的模拟消息队列之五——虚拟主机设计
    JavaScript中常用的输入输出语句介绍
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/127840808