杭电1195 Open the Lock
来源:互联网 发布:浮云网 淘宝号 编辑:程序博客网 时间:2024/05/22 17:11
题目大意:有一个紧急开启密码锁的任务。密码由四位数字组成;每个数字从1到9;每次,可以对每一个数字进行加1或者减1;当从1加到9时,由9再加1会变为1;当从9减到1时,由1再减1会变为9;也可以交换两个相邻的数字,每次操作作为一个step。你的任务就是用最少的步骤解锁!
#include <stdio.h> #include <algorithm> #include <string.h> #include <queue> using namespace std; struct node { int num[4],step; } first,last; int vis[11][11][11][11]; void bfs() { int i; node a,next; queue<node> q; a = first; a.step = 0; q.push(a); vis[a.num[0]][a.num[1]][a.num[2]][a.num[3]] = 1; while(!q.empty()) { a = q.front(); q.pop(); if(a.num[0] == last.num[0] && a.num[1] == last.num[1] && a.num[2] == last.num[2] && a.num[3] == last.num[3]) { printf("%d\n",a.step); return ; } for(i = 0; i<4; i++) //+1 { next = a; next.num[i]++; if(next.num[i]==10) next.num[i] = 1; if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1; next.step++; q.push(next); } } for(i = 0; i<4; i++) //-1 { next = a; next.num[i]--; if(next.num[i]==0) next.num[i] = 9; if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1; next.step++; q.push(next); } } for(i = 0; i<3; i++) //交换 { next = a; next.num[i] = a.num[i+1]; next.num[i+1] = a.num[i]; if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1; next.step++; q.push(next); } } } } int main() { int i,j,t; char s1[10],s2[10]; scanf("%d",&t); while(t--) { scanf("%s%s",s1,s2); for(i = 0; i<4; i++) { first.num[i] = s1[i]-'0'; last.num[i] = s2[i]-'0'; } memset(vis,0,sizeof(vis)); bfs(); } return 0; }
0 0
- 杭电1195 Open the Lock
- 杭电1195 open the lock (搜索题)BFS
- HDU-1195-Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 OPEN THE LOCK
- HDU-Open the Lock-1195
- POJ 1195 Open the Lock
- hdu 1195 Open the Lock
- hdu-1195-Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- hdu(1195) Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 Open the Lock
- poj 2336 DP
- oj14. Longest Common Prefix
- Java RMI 框架(远程方法调用)
- 一些常用的第三方框架之Fresco上篇
- 2009年程序员下半年上午试题
- 杭电1195 Open the Lock
- 设计模式——创建型模式之工厂模式
- Linux 多线程之Joinable和Detached
- 牛客刷题:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- xampp更改存放项目目录apache
- 基于链表的快速排序及归并排序
- 送邮件被退回,提示: Helo command rejected: Invalid name 错误,helorejected
- 2.2 一往直前!贪心法
- Codeforces Round #412 (Div. 2)(Codeforces 807 ABCDE)