1 条题解

  • 0
    @ 2025-4-12 22:06:17

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	
        int i,j,k,t1,t2,t,count,f=1,x; 
    	char a[5][6],m[100],tmp[100];
    	gets(a[0]);
    	while(a[0][0]!='Z')
    	{
    		count=0;
    		gets(a[1]);
    		gets(a[2]);    
    		gets(a[3]);
    		gets(a[4]);
    		gets(m);
    		x=strlen(m);
    		while(m[x-1]!='0')
    		{
    			scanf("%s",m+x);
    			gets(tmp);
    			x=strlen(m);
    		}
    		
    		t1=9;
    		t2=9;
    		for(j=0;j<5;j++)
    		{
    			for(k=0;k<5;k++)
    			{
    				if(a[j][k]==' ') 
    				{
    					t1=j;
    					t2=k;
    					break;
    				}
    			}
    			if(t1!=9&&t2!=9) break;
    		}
    		
    		j = t1;
    		k = t2;
    		
    		for(i=0;m[i]!='0';i++)
    		{
    			
    			switch(m[i])
    			{
    				case 'A': if(j>0)
    				          {
    				              t=a[j][k];
    				              a[j][k]=a[j-1][k];
    				              a[j-1][k]=t;
    				              j--;
    				          }
    				          else 
    						  {
    						  	if(f!=1) printf("\n");
    						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
    						  	  f++;
    						  	  count++;
    				          }break;
    				case 'B': if(j<5)
    				          {
    				              t=a[j][k];
    				              a[j][k]=a[j+1][k];
    				              a[j+1][k]=t;
    				              j++;
    				          }
    				          else 
    						  {
    						  	  if(f!=1) printf("\n");
    						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
    						  	  f++;
    						  	  count++;
    				          }break;
    				case 'R': if(k<5)
    				          {
    				              t=a[j][k];
    				              a[j][k]=a[j][k+1];
    				              a[j][k+1]=t;
    				              k++;
    				          }
    				          else 
    						  {
    						  	if(f!=1) printf("\n");
    						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
    						  	  f++;
    						  	  count++;
    				          }
    				          break;
    				case 'L': if(k>0)
    				          {
    				              t=a[j][k];
    				              a[j][k]=a[j][k-1];
    				              a[j][k-1]=t;
    				              k--;
    				          }
    				          else 
    						  {
    						  	if(f!=1) printf("\n");
    						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
    						  	  f++;
    						  	  count++;
    				          }break;
    			}
    			if(count!=0) break;
    		}
    		if(count==0) 
    		{
    			if(f!=1) printf("\n");
    			printf("Puzzle #%d:\n",f);
    	        for(i=0;i<5;i++)
    	        {
    	        	printf("%c %c %c %c %c\n",a[i][0],a[i][1],a[i][2],a[i][3],a[i][4]);
    	        }
    	        f++;
    		}
    		gets(a[0]);
    	}
    	return 0;
    }
    
    

    C++ :

    
    // UVa227
    // 2015-2-16
    
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    
    #define maxn 5
    
    // 输入命令行,可以解决跨行输入命令行的问题 
    void readcommand(string &str)
    {
    	getline(cin, str);
    	while(str[str.length()-1] != '0')
    	{
    		string temp_str;
    		getline(cin, temp_str);
    		str += temp_str;
    	}
    }
    
    // 判断移动后的位置合不合理
    int  reasonable_site(int x, int y)
    {
    	if(x < 0 || x > maxn-1 || y < 0 || y > maxn-1)
    	{
    		return 0;
    	}
    	return 1;
    }
    
    int main()
    {
    	int kcase = 0;
    	
    	// 用于存储信息 
    	string my_grid[maxn];
    	string my_command;	// 用于存储移动的命令 
    	
    	// 打开文件写入,提交的时候去掉 
    	// freopen("1.in", "r", stdin);
    	
    	while(1)
    	{
    		int x0, y0;		// 用于记录空格的下标 
    		
    		for(int i = 0; i < maxn; i++)
    		{
    			getline(cin, my_grid[i]);	// 这样读取以回车结束的空行,单独用cin也不读入空格 
    			if(my_grid[i][0] == 'Z') exit(0);	// 输入Z结束程序 
    			
    			// 找到空格
    			for(int j = 0; j < maxn; j++)
    			{
    				if(my_grid[i][j] == ' ')
    				{
    					x0 = i;
    					y0 = j;
    					break;
    				}
    			} 
    		}
    		
    		// 输入命令 
    		readcommand(my_command); 
    		
    		// 用于记录输入的命令是否合法
    		int flag = 1; 
    				
    		// 执行命令
    		for(int i = 0; i < my_command.length(); i++)
    		{
    			// 记录变换前的坐标 
    			int x1 = x0;
    			int y1 = y0;
    				
    			switch(my_command[i])
    			{
    				// A 表示向上x0-1 
    				case 'A': x0--; break;
    				case 'B': x0++; break;
    				case 'L': y0--; break;
    				case 'R': y0++;	break;
    				case '0': break;
    				default: flag = 0; break;		
    			}
    			
    			// 命令不合法或者位置不对则执行错误,进行下一次输入 
    			if(!flag || !reasonable_site(x0, y0))
    			{
    				break;
    			} 
    			else
    			{
    				char temp_ch = my_grid[x0][y0];
    				my_grid[x0][y0] = my_grid[x1][y1];
    				my_grid[x1][y1] = temp_ch;
    			}
    			
    		} 
    		
    		if(kcase)
    		{
    			// 每个Puzzle之间的回车
    			cout << endl; 
    		}
    		
    		// 输出结果 
    		cout << "Puzzle #" << ++kcase << ":" << endl;
    		if(!flag || !reasonable_site(x0, y0)) 
    		{
    			cout << "This puzzle has no final configuration." << endl;
    			continue;
    		}
    		
    		for(int i = 0; i < maxn; i++)
    		{
    			for(int j = 0; j < maxn; j++)
    			{
    				cout << my_grid[i][j];
    				if(j < maxn-1) cout << " ";
    			}
    			cout << endl;
    		}
    		
    	}
    	
    	return 0;
    }
    
    • 1

    信息

    ID
    2884
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者