标签:else ret inline tmp pre map front ble 源代码
#include <bits/stdc++.h>
using namespace std;
int end_state = 123804765;
map < int,bool > vis;
int a[10][10],x,y;
struct node {
int state,tm;
};
inline void build(int key){
if(key % 10 == 0)x = 2,y = 2;a[2][2] = key % 10;key /= 10;
if(key % 10 == 0)x = 2,y = 1;a[2][1] = key % 10;key /= 10;
if(key % 10 == 0)x = 2,y = 0;a[2][0] = key % 10;key /= 10;
if(key % 10 == 0)x = 1,y = 2;a[1][2] = key % 10;key /= 10;
if(key % 10 == 0)x = 1,y = 1;a[1][1] = key % 10;key /= 10;
if(key % 10 == 0)x = 1,y = 0;a[1][0] = key % 10;key /= 10;
if(key % 10 == 0)x = 0,y = 2;a[0][2] = key % 10;key /= 10;
if(key % 10 == 0)x = 0,y = 1;a[0][1] = key % 10;key /= 10;
if(key % 10 == 0)x = 0,y = 0;a[0][0] = key % 10;key /= 10;
}
inline int work(int direction,int state) {
int key = state;
build(key);
switch(direction){
case(1):if(x == 0) return key;swap(a[x][y],a[x - 1][y]);break;//上
case(2):if(x == 2) return key;swap(a[x][y],a[x + 1][y]);break;//下
case(3):if(y == 0) return key;swap(a[x][y],a[x][y - 1]);break;//左
case(4):if(y == 2) return key;swap(a[x][y],a[x][y + 1]);break;//右
}
key = 0;
for(int i = 0;i <= 2;i++){
for(int j = 0;j <= 2;j++){
key = key * 10 + a[i][j];
}
}
return key;
}
queue < node > q;
int main() {
int now_state;
cin>>now_state;
node t;
t.state = now_state;t.tm = 0;
q.push(t);
int tmp;
if(now_state == end_state)cout<<0,exit(0);
while(!q.empty()){
node now,use = q.front();q.pop();
now_state = work(1,use.state);
if(now_state == end_state){cout<<use.tm + 1;exit(0);}
else if(!vis[now_state]){vis[now_state] = true;q.push((node){now_state,use.tm + 1});}
now_state = work(2,use.state);
if(now_state == end_state){cout<<use.tm + 1;exit(0);}
else if(!vis[now_state]){vis[now_state] = true;q.push((node){now_state,use.tm + 1});}
now_state = work(3,use.state);
if(now_state == end_state){cout<<use.tm + 1;exit(0);}
else if(!vis[now_state]){vis[now_state] = true;q.push((node){now_state,use.tm + 1});}
now_state = work(4,use.state);
if(now_state == end_state){cout<<use.tm + 1;exit(0);}
else if(!vis[now_state]){vis[now_state] = true;q.push((node){now_state,use.tm + 1});}
}
return 0;
}
标签:else ret inline tmp pre map front ble 源代码
原文地址:https://www.cnblogs.com/czy--blog/p/11741849.html