1.HDU-1043-Eight:啥?康拓双映射,啥????
代码(待修改——几乎没啥进展):
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #define int long long
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define dbg(x) cout << #x << "===" << x << endl
#define ed(x, y) ((x)*3 + (y))
using namespace std;
void read(int &x) {
int res = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - '0'), c = getchar();
x = f * res;
}
void OUT(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) OUT(x / 10);
putchar(x % 10 + '0');
}
void print(int a, char c) { OUT(a), putchar(c); }
const int maxn = 2e2 + 10;
const int mod = 1e9 + 7;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
char mc[] = "durl", c;
int vis[10], sx, sy;
bool f1, f2,f;
bool ok(int x, int y) {
if (x >= 0 && x < 3 && y >= 0 && y < 3) return true;
return false;
}
void dfs(int *vis, int k, int x, int y) {
f=true;
for(int i=0;i<9;i++) if(vis[i]!=i+1) f=false;
if(f){
return ;
}
// f1 = true, f2 = true;
// for (int i = 0; i < 3; i++)
// if (vis[i] != i + 1) f1 = false;
// for (int i = 3; i < 6; i++)
// if (vis[i] != i + 1) f2 = false;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (ok(nx, ny)) {
swap(vis[ed(nx, ny)], vis[ed(x, y)]);
dfs(vis, k + 1, nx, ny);
swap(vis[ed(nx, ny)], vis[ed(x, y)]);
}
}
}
signed main() {
mst(vis, -1);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> c;
int tmp;
if (c >= '0' && c <= '9')
tmp = c - '0';
else
tmp = 9, sx = i, sy = j; // x->9
vis[ed(i, j)] = tmp;
}
}
dfs(vis, 0, sx, sy);
// print(ans,'\n');
return 0;
}