算法竞赛入门 -谜题
introduce
有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A, B, L, R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration.”
随便扯扯
不是个专门ac的人,只是无聊刷刷题,也没有特地去网站编译通过,只是看了下结果差不多。就算自己闲的无聊瞎写的吧
Code
- c++
#include<iostream>
using namespace std;
int main() {
cout << "请输入25个字符,默认是从左到右,从上到下,每个格子的字母, 空格用0表示" << endl;
string s;
cin >> s;
int arr[5][5];
int xpos = 0, ypos = 0;
// 判断是否超过界限
if (s.length() != 25) {
cout << s.length();
cout << "字符串的长度不对,请重新输入" << endl;
return -1;
} else {
// 初始化每一个位子
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
arr[i][j] = char(s[i * 5 + j]);
if ( arr[i][j] == '0') {
xpos = i;
ypos = j;
}
}
}
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
}
cout << "请输出移动规则" << endl;
string direction;
cin >> direction;
int len = int(direction.length());
for (int i = 0; i != len; i++) {
switch (direction[i]) {
case 'A':
case 'a':
if (xpos - 1 <0) {
cout << "越界"<< endl;
return -1;
} else {
int temp = arr[xpos][ypos];
arr[xpos][ypos] = arr[xpos-1][ypos];
arr[xpos - 1][ypos] = temp;
xpos = xpos - 1;
}
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
break;
case 'B':
case 'b':
if (xpos + 1 > 4) {
cout << "越界"<< endl;
return -1;
} else {
int temp = arr[xpos][ypos];
arr[xpos][ypos] = arr[xpos+1][ypos];
arr[xpos + 1][ypos] = temp;
xpos = xpos + 1;
}
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
break;
case 'L':
case 'l':
if (ypos - 1 <0) {
cout << "越界"<< endl;
return -1;
} else {
int temp = arr[xpos][ypos];
arr[xpos][ypos] = arr[xpos][ypos - 1];
arr[xpos][ypos - 1] = temp;
ypos = ypos - 1;
}
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
break;
case 'R':
case 'r':
if (ypos + 1 > 4) {
cout << "越界"<< endl;
return -1;
} else {
int temp = arr[xpos][ypos];
arr[xpos][ypos] = arr[xpos][ypos + 1];
arr[xpos][ypos + 1] = temp;
ypos = ypos+1;
}
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
break;
default:
cout << "结束" << endl;
for (int i = 0; i != 5; i++) {
for (int j = 0; j != 5; j++) {
cout << char(arr[i][j]) << " ";
}
cout << endl;
}
cout << "运动后" << endl;
break;
}
}
return 0;
}
- js
<script>
let detectLength = str => {
if (str.length != 25) {
console.log("字符串的长度不对")
return -1;
} else {
return 1;
}
}
let strToArr = str => {
var len = 5;
var newArr = [];
var reg = new RegExp(".{" + len + "}", "g");
newArr = str.match(reg);
var newArr = newArr.map(element => {
return element.split("");
});
show(newArr);
return newArr;
}
let show = arr => {
var str = "";
for (var i = 0; i != arr.length; i++) {
for (var j = 0; j != arr.length; j++) {
str += arr[i][j] + " ";
}
console.log(str);
str = "";
}
console.log("转换");
}
let findBegin = arr => {
var [xpos, ypos] = [0, 0];
for (var i = 0; i != arr.length; i++) {
for (var j = 0; j != arr.length; j++) {
if (arr[i][j] == '0') {
[xpos, ypos] = [i, j];
}
}
}
return [xpos, ypos];
}
let run = (dir, xpos, ypos) => {
switch (dir) {
case 'A':
case 'a':
if (xpos - 1 < 0) {
return 0;
} else {
xpos = xpos - 1;
}
break;
case 'B':
case 'b':
if (xpos + 1 > 4) {
return 0;
} else {
xpos = xpos + 1;
}
break;
case 'L':
case 'l':
if (ypos - 1 < 0) {
return 0;
} else {
ypos = ypos - 1;
}
break;
case 'R':
case 'r':
if (ypos + 1 > 4) {
return 0;
} else {
ypos = ypos + 1;
}
break;
default:
console.log("end");
return 0;
}
return [xpos, ypos];
}
let reverse = (arr, oldxpos, oldypos, xpos, ypos) => {
[arr[oldxpos][oldypos], arr[xpos][ypos]] = [arr[xpos][ypos], arr[oldxpos][oldypos]];
}
let main = (str, dir) => {
var xpos, ypos, oldxpos, oldypos;
if (detectLength(str)) {
var arr = strToArr(str);
[xpos, ypos] = findBegin(arr);
if (dir.length == 0) {
console.log("wrong");
return -1;
} else {
for (var i = 0; i != dir.length ;i ++) {
[oldxpos, oldypos] = [xpos, ypos];
try {
[xpos, ypos] = run(dir[i], xpos, ypos);
reverse(arr, oldxpos, oldypos, xpos, ypos);
show(arr);
}catch(e) {
console.log("越界");
}
}
}
}
}
main('ABCDEFG0HIJKLMNOPQRSTUVWX','ablraaaa');
</script>
Writer&Contact
{
"name":"Jontyy" ,
"email": " jontyy@163.com",
"gitHub" : "https://github.com/YJD199798/jontyy-ac"
}