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大概有几个呢??? 这个问题其实还 ...
随机推荐
- C语言随笔1:内存分配方式与动静态变量
首先几个基本概念(网上的各种说法都很乱:个人理解整理了一下 内存分类方法很多,动态.静态: 五区: 三段: 内存的区: 1.动态存储区分为 栈区.堆区 也统称为堆栈段1.1栈区(.stack) ...
- AOP使用
package com.googosoft.db.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lan ...
- Hdu6586 String 字符串字典序贪心
Hdu6586 字符串字典序贪心 题意 给出一个只包含26个字符的字符串(|S|<=1e5),要求从中取出长度为k的字典序最小的组序列,满足所给的26个字母的限制条件,例如\([l_i,r_i] ...
- Go网络编程TCP
1. 服务端 package main import ( "bufio" "fmt" "net" "os" " ...
- MyBatis知识点整理
1.MyBatis一般使用步骤 1.1获取Configuration实例或编写配置文件 //获取Configuration实例的样例 TransactionFactory transactionFac ...
- curl模板----php发送post,get请求
function _grab($curl,$ip='',$referer='',$postInfo='',$cookie=''){ $ch = curl_init(); curl_setopt($ch ...
- iview渲染函数
<Table border :columns="discountColumns" :data="discountData.rows"></Ta ...
- 如何在Access中使用SQL语句
如何在Access中使用SQL语句 创建,查询设计,弹出[显示表]窗口,点击[关闭]将该窗口关掉.这时软件会进入[设计]工具栏,我们点击工具栏左侧的[SQL视图].[SQL视图]默认选择的是[设计视图 ...
- .NTE Core Web API Example
Source from :https://www.codeproject.com/Articles/1260600/Speed-up-ASP-NET-Core-WEB-API-application- ...
- python正则匹配次数,贪婪和非贪婪
贪婪模式 {m,n}表示匹配子串的次数>=m and <=n,再此分为内匹配次数尽可能的多 贪婪模式 {,n}表示 >=0 and <=n 贪婪模式 {m,} 表示> ...