Gap HDU
来源:互联网 发布:988款淘宝爆款详情页 编辑:程序博客网 时间:2024/06/06 03:33
暴力bfs + map判重
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <queue>#include <cmath>#include <map>using namespace std;struct node{ char a[33]; int t;}s;char e[33] = {11, 12, 13, 14, 15, 16, 17, 1, 21, 22, 23, 24, 25, 26, 27, 1, 31, 32, 33, 34, 35, 36, 37, 1, 41, 42, 43, 44, 45, 46, 47, 1};void print(node t){ for (int k = 0; k < 32; k++) { if (k % 8 == 0) printf("\n"); printf("%d ", t.a[k]); }}int bfs(){ queue<node> q; s.t = 0; q.push(s); map<string, int> ma; int cnt = 0; ma[s.a] = ++cnt; if (ma[s.a] == ma[e]) return 0; ma[e] = ++cnt; while (!q.empty()) { node cur = q.front(); q.pop(); for (int i = 0; i < 32; i++) if (cur.a[i] == 1 && cur.a[i - 1] != 1 && cur.a[i - 1] % 10 != 7) { node tmp = cur; int val = cur.a[i - 1] + 1; for (int k = 0; k < 32; k++) if (tmp.a[k] == val) { tmp.a[k] = 1; tmp.a[i] = val; break; } tmp.t++; if (ma[tmp.a] == 2) return tmp.t; if (ma[tmp.a] == 0) { ma[tmp.a] = ++cnt; q.push(tmp); } } } return -1;}int main(){ int T; scanf("%d", &T); while (T--) { int k = 0, tmp; for (int i = 1; i <= 4; i++) { s.a[k++] = 1 + 10 * i; for (int j = 1; j <= 7; j++) { scanf("%d", &tmp); s.a[k] = tmp; if (tmp % 10 == 1) s.a[k] = 1; k++; } } printf("%d\n", bfs()); } return 0;}
阅读全文
0 0
- Gap HDU
- Gap HDU
- hdu 1067 Gap
- HDU 1067 GAP
- HDU-1067 GAP
- 【hdu 1067】Gap
- hdu 1067 Gap
- hdu 1067 Gap bfs+hash
- hdu 1067 Gap bfs+hash
- HDU-1067-Gap(BFS+HASH)
- hdu 1067 Gap bfs+hash
- [HDU 1067] Gap BFS+哈希
- hdu 1067 Gap+BFS+hash
- HDU 1067 Gap bfs+hash
- Gap
- GAP
- hdu 1067 && poj 2046 Gap (bfs+hash)
- HDU 1067 Gap(BFS+hash)
- 每日小记4
- 通过js得到时间,并显示到jsp上
- PAT 乙级练习题1005. 继续(3n+1)猜想 (25)
- sql之replace into的用法
- HDU 4289 Control (拆点+网络流)
- Gap HDU
- 领域驱动设计,读书笔记:5 分离领域
- opstion absolute怎么垂直居中
- 查看APK运行日志
- uva 11525 树状数组 求第k大数
- 字符串的替换python
- hdu 1244
- C++学习
- 基类指针指向派生类对象