hdu1195 Open the Lock(bfs水题)
2016-07-28 12:50
330 查看
题目:
Total Submission(s): 6124 Accepted Submission(s): 2742
Problem Description
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.
Input
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.
Output
For each test case, print the minimal steps in one line.
Sample Input
2
1234
2144
1111
9999
Sample Output
2
4
题意:
将一个4位数的密码从初始状态拨到正确状态,若从1往下拨,就变成9,若从9往上拨则变成0,有3种操作,向上拨,向下拨,或者是相邻的两个数字互换。每种操作记为一步,问最少需要多少步能得到正确密码。
思路:
很简单,模拟操作写个bfs即可。但是第一次写的时候用字符串写,结果各种问题,最后改成用数字写,写得一团糟。。。很久没有写过这么翔的代码了。。。
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6124 Accepted Submission(s): 2742
Problem Description
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.
Input
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.
Output
For each test case, print the minimal steps in one line.
Sample Input
2
1234
2144
1111
9999
Sample Output
2
4
题意:
将一个4位数的密码从初始状态拨到正确状态,若从1往下拨,就变成9,若从9往上拨则变成0,有3种操作,向上拨,向下拨,或者是相邻的两个数字互换。每种操作记为一步,问最少需要多少步能得到正确密码。
思路:
很简单,模拟操作写个bfs即可。但是第一次写的时候用字符串写,结果各种问题,最后改成用数字写,写得一团糟。。。很久没有写过这么翔的代码了。。。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node { int arr[4]; int t; }; int t; char first[5]; char second[5]; int startt[4]; int endd[4]; bool vis[10][10][10][10]; void bfs(){ queue<node> q; q.push(node{{startt[0], startt[1], startt[2], startt[3]}, 0}); vis[startt[0]][startt[1]][startt[2]][startt[3]] = true; node tmp1, tmp2, tmp3, tmp4, tmp5; while(!q.empty()){ tmp1 = tmp2 = tmp3 = tmp4 = tmp5 = q.front(); q.pop(); if(tmp1.arr[0]==endd[0] && tmp1.arr[1]==endd[1] &&tmp1.arr[2]==endd[2] && tmp1.arr[3]==endd[3]) {cout<<tmp1.t<<endl; return;} for(int i=0; i<4; i++){ tmp1 = tmp2 = tmp5; tmp1.arr[i] = (tmp1.arr[i]+1>9)?1:tmp1.arr[i]+1; if(!vis[tmp1.arr[0]][tmp1.arr[1]][tmp1.arr[2]][tmp1.arr[3]]){ tmp1.t++; q.push(tmp1); vis[tmp1.arr[0]][tmp1.arr[1]][tmp1.arr[2]][tmp1.arr[3]] = true; } tmp2.arr[i] = (tmp2.arr[i]-1<1)?9:tmp2.arr[i]-1; if(!vis[tmp2.arr[0]][tmp2.arr[1]][tmp2.arr[2]][tmp2.arr[3]]){ tmp2.t++; q.push(tmp2); vis[tmp2.arr[0]][tmp2.arr[1]][tmp2.arr[2]][tmp2.arr[3]] = true; } } swap(tmp3.arr[0], tmp3.arr[1]); if(!vis[tmp3.arr[0]][tmp3.arr[1]][tmp3.arr[2]][tmp3.arr[3]]){ tmp3.t++; q.push(tmp3); vis[tmp3.arr[0]][tmp3.arr[1]][tmp3.arr[2]][tmp3.arr[3]] = true; } swap(tmp4.arr[1], tmp4.arr[2]); if(!vis[tmp4.arr[0]][tmp4.arr[1]][tmp4.arr[2]][tmp4.arr[3]]){ tmp4.t++; q.push(tmp4); vis[tmp4.arr[0]][tmp4.arr[1]][tmp4.arr[2]][tmp4.arr[3]] = true; } swap(tmp5.arr[2], tmp5.arr[3]); if(!vis[tmp5.arr[0]][tmp5.arr[1]][tmp5.arr[2]][tmp5.arr[3]]){ tmp5.t++; q.push(tmp5); vis[tmp5.arr[0]][tmp5.arr[1]][tmp5.arr[2]][tmp5.arr[3]] = true; } } } int main(){ cin>>t; while(t--){ cin>>first; cin>>second; for(int i=0; i<4; i++){ startt[i] = first[i]-48; endd[i] = second[i]-48; } memset(vis, false, sizeof(vis)); bfs(); } return 0; }
相关文章推荐
- linux基础命令及注意点归纳
- 在 docker中 运行 mono /jexus server 并部署asp.net mvc站点
- ubuntu11.10下编译openwrt的环境配置
- ubuntu11.10下编译openwrt的环境配置
- ubuntu11.10下编译openwrt的环境配置
- ubuntu11.10下编译openwrt的环境配置
- ubuntu11.10下编译openwrt的环境配置
- ubuntu11.10下编译openwrt的环境配置
- linux 中切换用户:su和su -的使用环境变量详解
- 查看linux版本
- Linux入门笔记——文件操作命令2
- Doxygen安装、中文PDF输出全攻略(Linux + MacOS)
- 如何在linux下开启telnet服务
- Linux基础知识及简单命令
- Docker(1)——Docker快速入门
- Linux下查看文件和文件夹大小
- 解密微博红包:架构、防刷、监控和资源调度
- popuwindow 增加动画效果
- 一篇文章,读懂Netty的高性能架构之道
- 信号捕捉与模拟实现sleep()函数