阅读背景:

UVa - 227 - Puzzle(gets(),scanf("%c",&x))

来源:互联网 






思路:简单模拟,按照题目意思进行编码即可。


#include<cstdio>

using namespace std;
char m[5][5];
char cmd[100];
int main(){
//	freopen("input.txt","r",stdin);
	
	int b_x ,b_y ,cases=0;
	
	while(gets(m[0])){
		if(m[0][0]=='Z'){
			break;
		}else{
			
			for(int i=1 ;i<5 ;i++){
				gets(m[i]);
			}
			
			for(int i=0 ;i<5 ;i++){
				for(int j=0 ;j<5 ;j++){
					if(m[i][j] == ' '){
						b_x = i; b_y = j;
						break;
					}
				}	
			}
		}
			int count = 0;
			
			while(scanf("%c",&cmd[count])!=EOF){
				if(cmd[count]!='0'){
					count++;
				}else {
					break;
				}
			}

			cmd[count] = 0;
			getchar();
			
			int x=b_x ,y=b_y ,flag = 0;
			for(int i=0 ;cmd[i] ;i++){
				switch(cmd[i]){
					case 'A':x = b_x-1; y = b_y; break;
					case 'B':x = b_x+1; y = b_y; break;
					case 'L':x = b_x; y = b_y-1; break;
					case 'R':x = b_x; y = b_y+1; break;
				}
				
				if(x<0||x>4||y<0||y>4){
					flag = 1;
					break;
				}else{
					m[b_x][b_y] = m[x][y];
					m[x][y] =  ' ';
				  	b_x = x; b_y = y;
				}	
			}
			
			if(cases++){
				printf("\nPuzzle #%d:\n",cases);
			}else{
				printf("Puzzle #%d:\n",cases);
			}
			
			if(flag){
				printf("This puzzle has no final configuration.\n");
			}else{
				for(int i=0 ;i<5 ;i++){
					printf("%c",m[i][0]);
					for(int j=1 ;j<5 ;j++){
						printf(" %c",m[i][j]);
					}
					printf("\n");
				}
			}	
		
	}
	
	return 0;
}#i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: