制作重排九宫游戏c语言,九宫重排 (C++代码)

//使用哈希技术,提高检索效率,哈哈哈

#include

#include

using namespace std;

typedef int State[9];

const int MAXSTATE = 1000000;

State st[MAXSTATE], goal;

int dist[MAXSTATE];

const int MAXHASHSIZE = 1000003;

int head[MAXHASHSIZE], next[MAXSTATE];

void init_lookup_table() { memset(head, 0, sizeof(head)); }

int hash(State& s) {

int v = 0;

for(int i = 0; i 

return v % MAXHASHSIZE;

}

int try_to_insert(int s) {

int h = hash(st[s]);

int u = head[h];

while(u) {

if(memcmp(st[u], st[s], sizeof(st[s])) == 0) return 0;

u = next[u];

}

next[s] = head[h];

head[h] = s;

return 1;

}

const int dx[] = {-1, 1, 0, 0};

const int dy[] = {0, 0, -1, 1};

int bfs() {

init_lookup_table();

int front = 1, rear = 2;

while(front 

State& s = st[front];

if(memcmp(goal, s, sizeof(s)) == 0) return front;

int z;

for(z = 0; z 

int x = z/3, y = z%3;

for(int d = 0; d 

int newx = x + dx[d];

int newy = y + dy[d];

int newz = newx * 3 + newy;

if(newx >= 0 && newx = 0 && newy 

State& t = st[rear];

memcpy(&t, &s, sizeof(s));

t[newz] = s[z];

t[z] = s[newz];

dist[rear] = dist[front] + 1;

if(try_to_insert(rear)) rear++;

}

}

front++;

}

return 0;

}

int main() {

char s1[10],s2[10];

scanf("%s%s",s1,s2);

for(int i=0;i<9;i++){

if(s1[i]!='.') st[1][i]=s1[i]-'0'; else st[1][i]=0;

if(s2[i]!='.') goal[i]=s2[i]-'0'; else goal[i]=0;

}

int ans = bfs();

if(ans > 0) printf("%d\n", dist[ans]);

else printf("-1\n");

return 0;

}

解题思路:

参考代码:

#include

#include

using namespace std;

typedef int state[9];

const int maxn = 1000000;

const int hashsize = 1000003;

int dist[maxn];

int next[maxn];

int head[hashsize];

state st[maxn],goal;

void init(){

memset(head,0,sizeof(head));

}

int hash(state&s){

int v=0;

for(int i=0;i<9;i++)   v=v*10 + s[i];

return v % hashsize;

}

int try_to_insert(int s){

int h = hash(st[s]);

int u = head[h];

while(u){

if(memcmp(st[u],st[s],sizeof(st[s]))==0) return 0 ;

u=next[u];

}

next[s]=head[h];

head[h]=s;

return 1;

}

const int dx[]={0,0,1,-1};

const int dy[]={1,-1,0,0};

int bfs(){

init();

int rear = 2;

int front =1;

while(front < rear){

state &s = st[front];

if(memcmp(goal,s,sizeof(s))==0) return front ;

int z ;

for(z=0;z<9;z++) if(!s[z]) break;

int x = z / 3;

int y = z % 3;

for(int d=0;d<4;d++){

int newx = x+dx[d];

int newy = y+dy[d];

int newz = newx*3+newy;

if(newx >=0 && newx < 3 && newy>=0 && newy <3) {

state &t = st[rear];

memcpy(&t,&s,sizeof(s));

t[newz]=s[z];

t[z]=s[newz];

dist[rear]=dist[front]+1;

if(try_to_insert(rear)) rear++;

}

}

front++;

}

return 0;

}

int main(void){

char s1[10],s2[10];

scanf("%s%s",s1,s2);

for(int i=0;i<9;i++){

if(s1[i]!='.') st[1][i]=s1[i]-'0'; else st[1][i]=0;

if(s2[i]!='.') goal[i]=s2[i]-'0'; else goal[i]=0;

}

int ans = bfs();

if(ans) printf("%d\n",dist[ans]);

else printf("-1\n");

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值