eight(待考究)
为什么明明结果都可以到达那种情况,步骤不一样就不给通过
QAQ
有哪位大佬提点一下,在下感激不尽~~~
我的代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <string>
using namespace std;
int per[10]={1,1,2,6,24,120,720,5040,40320,362880};
int start[9],finish[9]={1,2,3,4,5,6,7,8,0};
bool dir[362880]={0};
int way[4][2]={0,1,1,0,0,-1,-1,0};
char owo[4]={ 'd','r','u' ,'l' };
class state{
public:
int position[9];
string step;
state(int arr[],string ss):step(ss){
for(int i=0;i<9;i++)position[i]=arr[i];
}
state(){}
};
bool cantor(int str[],int n){
int result=0;
for(int i=0;i<n;i++){
int counted=0;
for(int j=i+1;j<n;j++){
if(str[i]>str[j]){
counted++;
}
}
result+=counted*per[n-i-1];
}
if(!dir[result]){
dir[result]=1;
return 1;
}
return 0;
}
bool judge(int arr1[],int arr2[]){
for(int i=0;i<9;i++)
if(arr1[i]!=arr2[i])return 0;
return 1;
}
void bfs(){
queue<state> que;
que.push(state(start,string()));
cantor(start,9);
state now;
while(!que.empty()){
now=que.front();
que.pop();
int x,y,z,tx,ty,tz;//z为元素中0的位置,x,y为转化成图形第几行第几列
for(z=0;z<9;z++)
if(now.position[z]==0)
break;
x=z%3;
y=z/3;
for(int i=0;i<4;i++){
tx=x+way[i][0];
ty=y+way[i][1];
if(tx>=0&&tx<3&&ty<3&&ty>=0){
char tstep=owo[i];
tz=ty*3+tx;
swap(now.position[tz],now.position[z]);
if(judge(now.position,finish)){
cout << now.step+tstep << endl;
return;
}
if(cantor(now.position,9))
que.push(state(now.position,now.step+tstep));
swap(now.position[tz],now.position[z]);
}
}
}
cout << "unsolvable" << endl;
}
int main()
{
int i;
char ch,_,__;
for(i=0;i<9;i++){
if(i!=8)scanf("%c%c%c",&ch,&_,&__);
else scanf("%c",&ch);
if(ch=='x')start[i]=0;
else start[i]=ch-'0';
}
//for(i=0;i<9;i++)printf("%d\n",start[i]);
//for(i=0;i<9;i++)scanf("%d",&finish[i]);
bfs();
return 0;
}
然后一下代码用来验证:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main(){
string str;
char QAQ[9],_,__;
int way[4][2]={1,0,0,1,-1,0,0,-1};
char owo[4]={'r','d','l','u'};
for(int i=0;i<9;i++){
if(i<8)scanf("%c%c%c",&QAQ[i],&_,&__);
else scanf("%c",&QAQ[i]);
}
cin >> str;
int j;
for(j=0;j<9;j++){
if(QAQ[j]=='x')break;
}
int x,y,z=j,tx,ty,tz;
//for(string::iterator ite=str.begin();ite!=str.end();ite++)
for(j=0;j<str.size();j++)
{
x=z%3;
y=z/3;
for(int i=0;i<4;i++){
if(str[j]==owo[i]){//ite
tx=x+way[i][0];
ty=y+way[i][1];
if(tx>=0&&tx<3&&ty>=0&&ty<3){
tz=ty*3+tx;
swap(QAQ[z],QAQ[tz]);
z=tz;
break;
}
}
}
}
for(int i=0;i<9;i++)printf("%c ",QAQ[i]);
return 0;
}
eight(待考究)的更多相关文章
- 【MySQL】Create table 以及 foreign key 删表顺序考究。
1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...
- index ffs、index fs原理考究-1109
h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...
- OVF文件考究
一.什么是OVF文件 开源虚拟化格式OVF(不同于小写的ovf)文件是一种开源的文件规范,它描述了一个开源.安全.有效.可拓展的便携式虚拟打包以及软件分布格式,它一般有几个部分组成,分别是o ...
- 【HCNE题型自我考究】
H3CNE题目归结 制定标准 组织: 802.1X协议起源于标准的无线局域网协议802.11.主要目的是为了解决有线局域网用户的接入认证问题. 426.一个包含有华为等多厂商设备的交换网络,其VL ...
- 谈谈一些有趣的CSS题目(十二)-- 你该知道的字体 font-family
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- 浅谈java异常[Exception]
学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...
- SQL Server中SELECT会真的阻塞SELECT吗?
在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...
- 作为前端er,写在年末的一些话
写惯了技术类的文章,这种总结和唠嗑型的我基本也就在年末才来一篇.说实话,这种轻松类的文章往往比技术类的要受欢迎的多,因为其受众更广,看起来也不烧脑. 说来愧疚,这一年其实产出有点低,大致就写了不到二十 ...
- asp.net mvc 之旅 —— 第六站 ActionFilter的应用及源码分析
这篇文章我们开始看一下ActionFilter,从名字上其实就大概知道ActionFilter就是Action上的Filter,对吧,那么Action上的Filter大概有几个呢??? 这个问题其实还 ...
随机推荐
- 页面中<link>和<script>标签
在html中,经常肯定会有js,css的引入 <head> <title>MyHtml</title> <link rel="stylesheet& ...
- 【资源分享】Garry's mod 自制整合包
*----------------------------------------------[下载区]----------------------------------------------* ...
- 2019牛客多校第四场J free 最短路
free 题意 给出一个带权联通无向图,你需要从s走到t,你可以选择k条变让他们的权值为0问从s到t的最小权值是多少? 分析 思考一下,如果不带k条白嫖这个条件,那么这就是一个简单的dji就搞定了,我 ...
- Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览
一.设计原理 1.Hadoop架构: 流水线(PipeLine) 2.Hadoop架构: HDFS中数据块的状态及其切换过程,GS与BGS 3.Hadoop架构: 关于Recovery (Lease ...
- Maven安装与学习
一.安装 1.下载http://maven.apache.org/download.cgi 2.选择zip格式 安装完后解压到某一位置(E:\xitong\major\apache-maven-3.6 ...
- Codeforces Round #624 (Div. 3) C. Perform the Combo(前缀和)
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...
- kafka集群搭建记录
本文记录搭建kafka搭建过程. 一.硬件机器介绍 192.168.183.195 master-node 192.168.183.194 data-node1 192.168.183.196 dat ...
- 【DRF版本控制】
目录 使用内置的URLPathVersioning类 使用自定义的版本控制类 "首先,我们开发的项目会有多个版本. 其次,我们的项目版本会随着更新越来越多,我们不可能因出了新版本就不维护旧版 ...
- HttpClient与TestNG结合
1.HTTPclient插件的安装 在maven项目的pom.xml中引用HTTPclient包,如下 <dependencies> <dependency> <grou ...
- 台式机windows10 进入安全模式
按住shift键不松,在登录界面点击重启,即可进入安全模式!!!!