• poj1872A Dicey Problem



    284:28:39
    307:00:00
     
                               
    L - A Dicey Problem
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit  Status

    Description

    The three-by-three array in Figure 1 is a maze. A standard six-sided die is needed to traverse the maze (the layout of a standard six-sided die is shown in Figure 2). Each maze has an initial position and an initial die configuration. In Figure 1, the starting position is row 1, column 2-the "2" in the top row of the maze-and the initial die configuration has the "5" on top of the die and the "1" facing the player (assume the player is viewing the maze from the bottom edge of the figure). 

    To move through the maze you must tip the die over on an edge to land on an adjacent square, effecting horizontal or vertical movement from one square to another. However, you can only move onto a square that contains the same number as the number displayed on the top of the die before the move, or onto a "wild" square which contains a star. Movement onto a wild square is always allowed regardless of the number currently displayed on the top of the die. The goal of the maze is to move the die off the starting square and to then find a way back to that same square. 

    For example, at the beginning of the maze there are two possible moves. Since the 5 is on top of the die, it is possible to move down one square, and since the square to the left of the starting position is wild it is also possible to move left. If the first move chosen is to move down, this brings the 6 to the top of the die and moves are now possible both to the right and down. If the first move chosen is instead to the left, this brings the 3 to the top of the die and no further moves are possible. 

    If we consider maze locations as ordered pairs of row and column numbers (row, column) with row indexes starting at 1 for the top row and increasing toward the bottom, and column indexes starting at 1 for the left column and increasing to the right, the solution to this simple example maze can be specified as: (1,2), (2,2), (2,3), (3,3), (3,2), (3,1), (2,1), (1,1), (1,2). A bit more challenging example maze is shown in Figure 3. 

    The goal of this problem is to write a program to solve dice mazes. The input file will contain several mazes for which the program should search for solutions. Each maze will have either a unique solution or no solution at all. That is, each maze in the input may or may not have a solution, but those with a solution are guaranteed to have only one unique solution. For each input maze, either a solution or a message indicating no solution is possible will be sent to the output. 

    Input

    The input file begins with a line containing a string of no more than 20 non-blank characters that names the first maze. The next line contains six integers delimited by single spaces. These integers are, in order, the number of rows in the maze (an integer from 1 to 10, call this value R), the number of columns in the maze (an integer from 1 to 10, call this value C), the starting row, the starting column, the number that should be on top of the die at the starting position, and finally the number that should be facing you on the die at the starting position. The next R lines contain C integers each, again delimited by single spaces. This R * C array of integers defines the maze. A value of zero indicates an empty location in the maze (such as the two empty squares in the center column of the maze in Figure 3), and a value of -1 indicates a wild square. This input sequence is repeated for each maze in the input. An input line containing only the word "END" (without the quotes) as the name of the maze marks the end of the input.

    Output

    The output should contain the name of each maze followed by its solution or the string "No Solution Possible" (without the quotes). All lines in the output file except for the maze names should be indented exactly two spaces. Maze names should start in the leftmost column. Solutions should be output as a comma-delimited sequence of the consecutive positions traversed in the solution, starting and ending with the same square (the starting square as specified in the input). Positions should be specified as ordered pairs enclosed in parentheses. The solution should list 9 positions per line (with the exception of the last line of the solution for which there may not be a full 9 positions to list), and no spaces should be present within or between positions.

    Sample Input

    DICEMAZE1
    3 3 1 2 5 1
    -1 2 4
    5 5 6
    6 -1 -1
    DICEMAZE2
    4 7 2 6 3 6
    6 4 6 0 2 6 4
    1 2 -1 5 3 6 1
    5 3 4 5 6 4 2
    4 1 2 0 3 -1 6
    DICEMAZE3
    3 3 1 1 2 4
    2 2 3
    4 5 6
    -1 -1 -1
    END
    

    Sample Output

    DICEMAZE1
      (1,2),(2,2),(2,3),(3,3),(3,2),(3,1),(2,1),(1,1),(1,2)
    DICEMAZE2
      (2,6),(2,5),(2,4),(2,3),(2,2),(3,2),(4,2),(4,1),(3,1),
      (2,1),(2,2),(2,3),(2,4),(2,5),(1,5),(1,6),(1,7),(2,7),
      (3,7),(4,7),(4,6),(3,6),(2,6)
    DICEMAZE3
      No Solution Possible
    
    用了个bfs就过了,水题,在最后格式输出的时候错了几次,做题的时候,不防做个小正方形,这样省去了大量的想象,速度也快的多!
    #include <iostream>
    #include  <stdio.h>
    #include<string.h>
    #include<string>
    #include <queue>
    using namespace std;
    int row,column,up,down,n,m,left,right,front,back,startx,starty;
    struct mazntree{int x[7];string re;int row,colomn;};
    mazntree start,p,temp;
    int visit[7][7][50][50];
    int map[100][100];
    int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
    int init()
    {
        scanf("%d%d%d%d%d%d",&n,&m,&startx,&starty,&up,&front);
        if(up==1)
        {
            if(front==2)
            {
                start.x[1]=1,start.x[2]=6,start.x[3]=4,start.x[4]=3,start.x[5]=2,start.x[6]=5;
            }
            else if(front==3)
            {
                 start.x[1]=1,start.x[2]=6,start.x[3]=2,start.x[4]=5,start.x[5]=3,start.x[6]=4;
            }
            else if(front==4)
            {
                 start.x[1]=1,start.x[2]=6,start.x[3]=5,start.x[4]=2,start.x[5]=4,start.x[6]=3;
            }
            else if( front==5)
            {
                 start.x[1]=1,start.x[2]=6,start.x[3]=3,start.x[4]=4,start.x[5]=5,start.x[6]=2;
            }
        }
    	else if(up==2)
        {
            if(front==1)
            {
                start.x[1]=2,start.x[2]=5,start.x[3]=3,start.x[4]=4,start.x[5]=1,start.x[6]=6;
            }
            else if(front==3)
            {
                 start.x[1]=2,start.x[2]=5,start.x[3]=6,start.x[4]=1,start.x[5]=3,start.x[6]=4;
            }
            else if(front==4)
            {
                 start.x[1]=2,start.x[2]=5,start.x[3]=1,start.x[4]=6,start.x[5]=4,start.x[6]=3;
            }
            else if( front==6)
            {
                 start.x[1]=2,start.x[2]=5,start.x[3]=4,start.x[4]=3,start.x[5]=6,start.x[6]=1;
            }
        }
    	else if(up==3)
        {
            if(front==1)
            {
                start.x[1]=3,start.x[2]=4,start.x[3]=5,start.x[4]=2,start.x[5]=1,start.x[6]=6;
            }
            else if(front==2)
            {
                 start.x[1]=3,start.x[2]=4,start.x[3]=1,start.x[4]=6,start.x[5]=2,start.x[6]=5;
            }
            else if(front==5)
            {
                 start.x[1]=3,start.x[2]=4,start.x[3]=6,start.x[4]=1,start.x[5]=5,start.x[6]=2;
            }
            else if( front==6)
            {
                 start.x[1]=3,start.x[2]=4,start.x[3]=2,start.x[4]=5,start.x[5]=6,start.x[6]=1;
            }
        }
    	else if(up==4)
        {
            if(front==1)
            {
                start.x[1]=4,start.x[2]=3,start.x[3]=2,start.x[4]=5,start.x[5]=1,start.x[6]=6;
            }
            else if(front==2)
            {
                 start.x[1]=4,start.x[2]=3,start.x[3]=6,start.x[4]=1,start.x[5]=2,start.x[6]=5;
            }
            else if(front==5)
            {
                 start.x[1]=4,start.x[2]=3,start.x[3]=1,start.x[4]=6,start.x[5]=5,start.x[6]=2;
            }
            else if( front==6)
            {
                 start.x[1]=4,start.x[2]=3,start.x[3]=5,start.x[4]=2,start.x[5]=6,start.x[6]=1;
            }
        }
         else if(up==5)
        {
            if(front==1)
            {
                start.x[1]=5,start.x[2]=2,start.x[3]=4,start.x[4]=3,start.x[5]=1,start.x[6]=6;
            }
            else if(front==3)
            {
                 start.x[1]=5,start.x[2]=2,start.x[3]=1,start.x[4]=6,start.x[5]=3,start.x[6]=4;
            }
            else if(front==4)
            {
                 start.x[1]=5,start.x[2]=2,start.x[3]=6,start.x[4]=1,start.x[5]=4,start.x[6]=3;
            }
            else if( front==6)
            {
                 start.x[1]=5,start.x[2]=2,start.x[3]=3,start.x[4]=4,start.x[5]=6,start.x[6]=1;
            }
        }
    	else if(up==6)
        {
            if(front==2)
            {
                start.x[1]=6,start.x[2]=1,start.x[3]=3,start.x[4]=4,start.x[5]=2,start.x[6]=5;
            }
            else if(front==3)
            {
                 start.x[1]=6,start.x[2]=1,start.x[3]=5,start.x[4]=2,start.x[5]=3,start.x[6]=4;
            }
            else if(front==4)
            {
                 start.x[1]=6,start.x[2]=1,start.x[3]=2,start.x[4]=5,start.x[5]=4,start.x[6]=3;
            }
            else if( front==5)
            {
                 start.x[1]=6,start.x[2]=1,start.x[3]=4,start.x[4]=3,start.x[5]=5,start.x[6]=2;
            }
        }
        int i,j;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
    	return 1;
    }
    bool changecan(int i)//判定能否走
    {
        int x[7];
        for( int j=1;j<7;j++)
        {
            x[j]=p.x[j];
        }
        p.row=p.row+dir[i][0];
        p.colomn=p.colomn+dir[i][1];
        if((map[p.row][p.colomn]!=0)&&p.row>=1&&p.row<=n&&p.colomn>=1&&p.colomn<=m&&((p.x[1]==map[p.row][p.colomn])||(map[p.row][p.colomn]==-1)))//不越界
        {
    
           if( i==0)//左移
           {
               p.x[3]=x[1];
               p.x[4]=x[2];
               p.x[2]=x[3];
               p.x[1]=x[4];
               p.re=temp.re+'0';
           }
           else if(i==1)//下称 
           {
               p.x[5]=x[1];
               p.x[6]=x[2];
               p.x[2]=x[5];
               p.x[1]=x[6];
               p.re=temp.re+'1';
    
           }
           else if ( i==2)//右移
           {
               p.x[4]=x[1];
               p.x[3]=x[2];
               p.x[1]=x[3];
               p.x[2]=x[4];
               p.re=temp.re+'2';
    
           }
           else if (i==3)//上移
           {
               p.x[6]=x[1];
               p.x[5]=x[2];
               p.x[1]=x[5];
               p.x[2]=x[6];
               p.re=temp.re+'3';
            }
           if(visit[p.x[1]][p.x[5]][p.row][p.colomn]==0)//没有访问过-1能走,0不能走
            {
                visit[p.x[1]][p.x[5]][p.row][p.colomn]=1;//标记为已访问
                 return true;
            }
           else
             return false;
        }
        else
        {
            return false;
        }
    	return false;
    }
    int bfs()
    {
        queue<mazntree> q;
        int i,xx,yy;
        memset(visit ,0,sizeof(visit));
    	while(!q.empty())//先清空
    	{
    		q.pop();
        }
        start.re="";
        start.colomn=starty;
        start.row=startx;
        q.push(start);
    
        while(!q.empty())
        {
            temp=q.front();
            q.pop();
            for( i=0;i<4;i++)
            {
                p=temp;
               if(changecan(i))
               {
    
                    q.push(p);
    			if(visit[p.x[1]][p.x[5]][p.row][p.colomn]&&(p.row==startx)&&(p.colomn==starty))//找到
    			  {
    
                int count=0;
                xx=startx,yy=starty;
    			count=1;
    			printf("  (%d,%d),",startx,starty);
    
    
                for( i=0;i<p.re.length()-1;i++,count++)
                {
                    xx=xx+dir[p.re[i]-'0'][0];
                    yy=yy+dir[p.re[i]-'0'][1];
                    if(count==0)
    
                    printf("  ");
    
                    printf("(%d,%d),",xx,yy);
    
                    if(count==8)
                    {
                        printf("
    ");
                        count=-1;
                    }
                }
    			 xx=xx+dir[p.re[i]-'0'][0];
                    yy=yy+dir[p.re[i]-'0'][1];
    				printf("(%d,%d)
    ",xx,yy);
    
    
                return 1;
            }
               }
            }
    }
        printf("  No Solution Possible
    ");
        return -1;
    }
    int main()
    {
        char str[200];
        while(1)
        {
    
            gets(str);
            if(str[0]=='E'&&str[1]=='N'&&str[2]=='D')
                break;
          init();
            printf("%s
    ",str);
           gets(str);//把最后的回车吸收
           bfs();
        }
        return 0;
    }
    



    FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project
    All Copyright Reserved ©2010-2012  HUST ACM/ICPC TEAM 
    Anything about the OJ, please ask in the  forum, or contact author: Isun
    Server Time: 
  • 相关阅读:
    http://wpftoolkit.codeplex.com/t的使用方法
    WPF自定义控件Textbox 带水印 以及错误信息显示_02
    WPF自定义控件Textbox 带水印 以及错误信息显示
    WPF嵌入WORD文档
    WPF ObservableCollection 使用方法
    react 开发 next.js rn
    获取百度地图sdk
    使用rn如何在安卓App开发中去增删改查对数据进行本地操作和保存,利用插件【react-native-file-selector】UI体现上传文件,和react-native-fs,可以对本地文件进行操作
    python常见命令
    py 文件设置编码
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3190059.html
Copyright © 2020-2023  润新知