hdu 1195 Open the Lock(bfs)
2015-10-02 16:49
459 查看
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5355 Accepted Submission(s): 2386
[align=left]Problem Description[/align]
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
[align=left]Input[/align]
The input file begins with an integer T, indicating the number of test cases.
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
[align=left]Output[/align]
For each test case, print the minimal steps in one line.
[align=left]Sample Input[/align]
2
1234
2144
1111
9999
[align=left]Sample Output[/align]
2
4
[align=left]Author[/align]
YE, Kai
[align=left]Source[/align]
Zhejiang University Local Contest 2005
比较水...直接bfs就好.
妈蛋一直交错题目QAQ
/************************************************************************* > File Name: code/hdu/1195.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月02日 星期五 14时42分21秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz using namespace std; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; int x,y; bool vis[10005]; struct Q { int g,s,b,q; int d; }s,tar; int add( int x) { int res = 0 ; res = (x+1); if (res == 10) res = 1; return res; } int minu( int x) { int res = 0 ; res = (x-1); if (res==0) res = 9; return res; } int get(Q a) { int res = 0 ; res = a.q*1000+a.b*100+a.s*10+a.g; return res; } void bfs() { queue<Q>q; q.push(s); while (!q.empty()) { Q pre = q.front();q.pop(); // cout<<get(pre)<<" "<<pre.d<<endl; if (get(pre)==y) { printf("%d\n",pre.d); // cout<<"aaahhh"<<endl; return; } Q nxt = pre; nxt.d = pre.d + 1; nxt.g = add(pre.g); if (!vis[get(nxt)]) { vis[get(nxt)] = true; // nxt.d = pre.d +1; q.push(nxt); } nxt.g = minu(pre.g); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] =true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.s = add(pre.s); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)]=true; q.push(nxt); } nxt.s = minu(pre.s); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] =true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.b =add(pre.b); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } nxt.b =minu(pre.b); if (!vis[get(nxt)]) { //nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.q =add(pre.q); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } nxt.q = minu(pre.q); if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)]= true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.g = pre.s; nxt.s = pre.g; if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.s = pre.b; nxt.b = pre.s; if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } nxt = pre; nxt.d = pre.d+1; nxt.b = pre.q; nxt.q = pre.b; if (!vis[get(nxt)]) { // nxt.d = pre.d +1; vis[get(nxt)] = true; q.push(nxt); } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int T; scanf("%d",&T); while (T--) { memset(vis,false,sizeof(vis)); scanf("%d %d",&x,&y); s.g = x %10; s.s = x%100/10; s.b = x/100%10; s.q = x/1000; s.d = 0 ; vis[x] = true; bfs(); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- AOJ 2456 Usoperanto (树形dp)解题报告
- Linux 命令 - tail: 打印文件的结尾部分
- web应用配置的logback,为什么在tomcat下没日志文件出现
- Linux 命令 - head: 打印文件的开头部分
- 如何用tomcat配置虚拟目录,方法要详细明了
- 《深入剖析Tomcat》阅读(一)
- linux文件处理命令——目录处理命令
- Nginx Event模块
- 许鹏:从零开始学习,Apache Spark源码走读(三)
- Tomcat中的JSTL配置与使用
- Linux基本命令(1)
- 解决docker镜像无法下载的问题
- Linux查看和杀死进程
- linux(ubuntu)中一些特殊符号
- Linux 命令 - uniq: 通知或忽略重复行
- Linux安装与配置samb服务器的过程以及遇到的问题
- [Linux.Shell] 变量提取
- 如何在Linux上使用netstat命令查证DDOS攻击
- Centos6.5下一个Ceph存储集群结构
- Set Up VTune Amplifier(windows) 2015 for Remote (linux)Analysis