zoukankan      html  css  js  c++  java
  • poj 1872 A Dicey Problem WA的代码,望各位指教!!!

    A Dicey Problem
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 832   Accepted: 278

    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
    

    Source


    这个题让我很伤心,贴一个深搜的代码,是WA的,检查很多遍了都不知道错哪了,望大神赐教啊,哪组数据有问题说说也可以啊,我测了我能找到的所有数据都没发现问题,实在是不想留下这个遗憾啊
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    int r,c,map[15][15],vis[15][15][7][7],die[][6]={{7,4,2,5,3,7},{3,7,6,1,7,4},{5,1,7,7,6,2},{2,6,7,7,1,5},{4,7,1,6,7,3},{7,3,5,2,4,7}};//die[i][j]表示i+1为top j+1为face时骰子左边的点数
    int sr,sc;//face[7]={0,6,5,4,3,2,1},
    int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0},w,deep;
    char name[25];
    
    struct point
    {
    	int i,j,fi,fj;
    }s[3000];
    
    int bound(int y,int x)
    {
    	if(x>=1&&x<=c&&y>=1&&y<=r)
    		return 1;
    	else
    		return 0;
    }
    
    int dfs(struct point a,int facenum,int topnum)//r是行c是列
    {
    	int i,j;
    	if(a.i==sr&&a.j==sc&&deep>0)
    		return 1;
    	deep++;
    	for(i=0;i<4;i++)
    	{
    		if(bound(a.i+dy[i],a.j+dx[i])&&(map[a.i+dy[i]][ a.j+dx[i]]==-1||map[a.i+dy[i]][a.j+dx[i]]==topnum))
    		{
    			if(i<2)
    			{
    				if(i==0)
    				{
    					if(!vis[a.i+1][a.j][topnum][7-facenum])//表示向下滚(i增大)
    					{
    						vis[a.i+1][a.j][topnum][7-facenum]=1;
    						s[w].i=a.i+1;
    						s[w].j=a.j;//s[w].fi=a.i;s[w].fj=a.j;
    						w++;
    						if(dfs(s[w-1],topnum,7-facenum))
    						{
    						//	printf("(%d,%d)",a.i,a.j);
    							return 1;
    						}
    						else w--;
    					}
    				}
    				else
    				{
    					if(!vis[a.i-1][a.j][7-topnum][facenum])//向上滚(也就是i减小的方向)
    					{
    						vis[a.i-1][a.j][7-topnum][facenum]=1;
    						s[w].i=a.i-1;
    						s[w].j=a.j;//s[w].fi=a.i;s[w].fj=a.j;
    						w++;
    						if(dfs(s[w-1],7-topnum,facenum))
    						{
    						//	printf("(%d,%d)",a.i,a.j);
    							return 1;
    						}
    						else w--;
    					}
    				}
    			}
    			else
    			{
    				if(i==2)
    				{
    					if(!vis[a.i][a.j+1][facenum][die[topnum-1][facenum-1]])//向右滚
    					{
    						vis[a.i][a.j+1][facenum][die[topnum-1][facenum-1]]=1;
    						s[w].i=a.i;
    						s[w].j=a.j+1;//s[w].fi=a.i;s[w].fj=a.j;
    						w++;
    						if(dfs(s[w-1],facenum,die[topnum-1][facenum-1]))
    						{
    						//	printf("(%d,%d)",a.i,a.j);
    							return 1;
    						}
    						else w--;
    					}
    				}
    				else
    				{
    					if(!vis[a.i][a.j-1][facenum][7-die[topnum-1][facenum-1]])//向左滚
    					{
    						vis[a.i][a.j-1][facenum][7-die[topnum-1][facenum-1]]=1;
    						s[w].i=a.i;
    						s[w].j=a.j-1;//s[w].fi=a.i;s[w].fj=a.j;
    						w++;
    						if(dfs(s[w-1],facenum,7-die[topnum-1][facenum-1]))
    						{
    						//	printf("(%d,%d)",a.i,a.j);
    							return 1;
    						}
    						else w--;
    					}
    				}
    			}
    		}
    	}
    	return 0;
    }
    
    int scan()//优化输入的
    {  
        char c;  
        int ret;  
        int sig=0;  
        while(isspace(c=getchar()))  
            ;  
        if(c=='-')  
        {  
            sig=1;  
            c=getchar();  
        }  
        ret=c-'0';  
        while((c=getchar())>='0'&& c<='9')  
            ret=ret*10+c-'0';  
        return sig?-ret:ret;  
    }  
    int main()
    {
    	int i,j,k,ft,ff;
    	while(1)
    	{
    		gets(name);
    		if(strcmp(name,"END")==0)
    			break;
    		//scanf("%d%d%d%d%d%d",&r,&c,&sr,&sc,&ft,&ff);
    		r=scan();
    		c=scan();
    		sr=scan();
    		sc=scan();
    		ft=scan();
    		ff=scan();
    		for(i=1;i<=r;i++)
    			for(j=1;j<=c;j++)
    			{
    				//scanf("%d",&map[i][j]);
    				map[i][j]=scan();
    			}
    		//getchar();
    		memset(s,0,sizeof(s));
    		memset(vis,0,sizeof(vis));
    		s[0].i=sr;
    		s[0].j=sc;
    		w=1;
    		puts(name);
    		deep=0;
    		if(dfs(s[0],ff,ft))
    		{
    			printf("  ");
    			for(i=0;i<w-1;i++)
    			{
    				printf("(%d,%d),",s[i].i,s[i].j);
    				if((i+1)%9==0)
    				{
    					printf("
      ");
    				}
    			}
    			printf("(%d,%d)
    ",s[i].i,s[i].j);
    		}
    		else printf("  No Solution Possible
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    冰淇淋主题博客皮肤
    在input内放置小图标的方法
    制作表单(第一期笔记):美化表单常用的css样式
    CSS:linear-gradient()背景颜色渐变--练习笔记
    css中的vertical-align在内联元素中的表现--垂直对齐(带图示)
    利用border特性做的几个纯css小图标
    CSS文本、字体个别属性效果对比
    CSS文本、字体属性(更新中)
    JavaScript HTML DOM 学习笔记
    JS、JQ获取容器内标签的个数
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3190022.html
Copyright © 2011-2022 走看看