UVa 227 Puzzle

227 Puzzle

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame also contained an empty position which was the same size as a small square. A square could be moved into that empty position if it were immediately to the right, to the left, above, or below the empty position. The object of the puzzle was to slide squares into the empty position so that the frame displayed the letters in alphabetical order.

The illustration below represents a puzzle in its original configuration and in its configuration after the following sequence of 6 moves:

1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty position moves. 

这道题的看着是比较简单的一道,但是对于代码来说还是有很多坑

首先是对数据的存储问题,怎么存?最先考虑到的是用二维字符数组,然后用cin进行输入
这就有问题了,cin输入它不接收空格,如立即对接收到的字符矩阵进行输出,就没有空格输出出来
所以需要换一种输出方式,改成用scanf输入,这样就可以读取空格字符了。为什么?还不知道

还需要注意的是字符数组的每一行末尾是一个回车,需要用getchar吸收回车,不然的话会导致后面的混乱

除了这个地方需要用getchar(),还有一个用的地方是读入操作的时候,读入操作这个就很皮了
它可以不写在一行,也就是说不能整行地读入,我的操作是定义一个字符,然后一个一个将字符读入,读入后放入字符数组中,连成一个字符串
之后整体进行交换的操作

进行调换之前还得判断一下可不可以调换,如果发生数组越界了就不能进行调换

其实一开始我写的是在整个外面进行判断,但是这样写有些不好,就是明明已经发生越界了却还要进行调换

其他的倒没什么可以注意的了,但是这道题还是一直过不了。。。。

明天再看吧

以下代码

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

int main(){
	int count=0;
	while(1){
		count++;//计数 
		int I,J;
		char A[6][6];
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				scanf("%c",&A[i][j]);
				if(A[1][1]=='Z'){
					return 0;
				}	
			}
			getchar();
		}//输入数组数据  
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				if(A[i][j]==' '){
					I=i;
					J=j;
				}
			}
		}
		char c;
		char s[100];
		int cnt=0;
		while(scanf("%c",&c)&&c!='0'){
			if (c=='\n') continue;
			s[cnt++]=c;
		}
		getchar();
		//for()
		//cout<<s<<endl;
		//cout<<"&&&&&&&&&&&&&&   输入操作       &&&&&&&&&&&&&&&&&&&&"<<endl; 
		//getchar();
		bool flag=1;//用于标记操作是否合法 
		//cout<<"flag1="<<flag<<endl;
		for(int i=0;i<=cnt;i++){
			if(s[i]!='0'&&flag==1){
			//cout<<"I="<<I<<' '<<"J="<<J<<endl;
				char t;
				
				if(s[i]=='A'){
					if(I-1<1){
						flag=0;
						break;
				    }  
					t=A[I][J];
					A[I][J]=A[I-1][J];
					A[I-1][J]=t;
					I-=1;
				}else if(s[i]=='B'){
					if(I+1>5){
						flag=0;
						break;
				    }  
					t=A[I][J];
					A[I][J]=A[I+1][J];
					A[I+1][J]=t;
					I+=1;
				}else if(s[i]=='R'){
					if(J+1>5){
						flag=0;
						break;
				    }  
					t=A[I][J];
					A[I][J]=A[I][J+1];
					A[I][J+1]=t;
					J+=1;
				}else if(s[i]=='L'){
					if(J-1<1){
						flag=0;
						break;
				    }  
					t=A[I][J];
					A[I][J]=A[I][J-1];
					A[I][J-1]=t;
					J-=1;
				}//对数组中的元素进行调换 
				
			}
		}
		if(count!=1){
			cout<<endl;
		}
		cout<<"Puzzle #"<<count<<':'<<endl;
		if(!flag){
			cout<<"This puzzle has no final configuration."<<endl;
		}else{
			for(int i=1;i<=5;i++){ 
				for(int j=1;j<=5;j++){
					cout<<A[i][j]<<' ';
				}
				cout<<endl; 
			}
		}//输出调换后的数组元素 
		//cout<<endl;
	}
	return 0;
}
/*
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAAAABBRRRLL0
Z

*/

需要真正弄明白每个函数都有什么作用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单词搜索迷宫(Word Search Puzzle)问题是一个经典的算法问题,其输入是一个二维的字符数组和一组单词,目标是找出字符数组网格中的所有单词。这些单词可以是水平的、垂直的或者是任意的对角线方向,所以需要查找8个不同的方向。解决这个问题的一种常见方法是使用回溯算法,具体步骤如下: 1. 遍历二维字符数组,对于每个字符,以其为起点开始搜索,搜索的方向包括水平、垂直和对角线方向。 2. 对于每个搜索到的单词,将其记录下来。 3. 重复步骤1和2,直到遍历完整个二维字符数组。 下面是一个使用C#语言实现的单词搜索迷宫算法的示例代码: ```csharp class WordSearchPuzzle { private char[,] grid; private HashSet<string> words; public WordSearchPuzzle(char[,] grid, HashSet<string> words) { this.grid = grid; this.words = words; } public void Solve() { int rows = grid.GetLength(0); int cols = grid.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Search(i, j, new StringBuilder()); } } } private void Search(int row, int col, StringBuilder sb) { if (row < 0 || row >= grid.GetLength(0) || col < 0 || col >= grid.GetLength(1)) { return; } sb.Append(grid[row, col]); string word = sb.ToString(); if (words.Contains(word)) { Console.WriteLine("Found '{0}' at [{1}, {2}] to [{3}, {4}]", word, row, col, row - sb.Length + 1, col - sb.Length + 1); } if (word.Length < 3) { Search(row + 1, col, sb); Search(row - 1, col, sb); Search(row, col + 1, sb); Search(row, col - 1, sb); Search(row + 1, col + 1, sb); Search(row - 1, col - 1, sb); Search(row + 1, col - 1, sb); Search(row - 1, col + 1, sb); } sb.Remove(sb.Length - 1, 1); } } // 使用示例 char[,] grid = new char[,] { {'t', 'h', 'i', 's'}, {'w', 'a', 't', 's'}, {'o', 'a', 'h', 'g'}, {'f', 'g', 'd', 't'} }; HashSet<string> words = new HashSet<string>() { "this", "two", "fat", "that" }; WordSearchPuzzle puzzle = new WordSearchPuzzle(grid, words); puzzle.Solve(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值