HDU1195
来源:互联网 发布:centos编译c 编辑:程序博客网 时间:2024/06/02 02:44
Problem: Open the Lock
Description: 开一把四位数的密码锁。你可以把任意一个数字加一或减一。如果变成0就是9,如果是10就是1,你也可以交换相邻的两个数字。问需要最少的步数把一把锁解开。
Code(C++):
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>using namespace std;typedef struct tagNode{ char digit[5]; int step; tagNode(){} tagNode(char _digit[],int _step): step(_step){ strcpy(digit,_digit); }}Node;char src[5],des[5];bool used[10000];int bfs(){ queue<Node> que; memset(used,false,sizeof(used)); que.push(Node(src,0)); used[atoi(src)]=true; while(!que.empty()){ Node now=que.front(); que.pop(); char tmp[5]; for(int i=0;i<4;i++){ strcpy(tmp,now.digit); int d=tmp[i]-'0'; ++d; if(d==10) d=1; tmp[i]=d+'0'; if(!strcmp(tmp,des)) return now.step+1; int flag=atoi(tmp); if(!used[flag]) used[flag]=true, que.push(Node(tmp,now.step+1)); } for(int i=0;i<4;i++){ strcpy(tmp,now.digit); int d=tmp[i]-'0'; --d; if(!d) d=9; tmp[i]=d+'0'; if(!strcmp(tmp,des)) return now.step+1; int flag=atoi(tmp); if(!used[flag]) used[flag]=true, que.push(Node(tmp,now.step+1)); } for(int i=0;i<3;i++){ strcpy(tmp,now.digit); char c=tmp[i]; tmp[i]=tmp[i+1]; tmp[i+1]=c; if(!strcmp(tmp,des)) return now.step+1; int flag=atoi(tmp); if(!used[flag]) used[flag]=true, que.push(Node(tmp,now.step+1)); } } return -1;}int main(){ int N; for(scanf("%d",&N);N--;){ scanf("%s%s",src,des); int ans=bfs(); printf("%d\n",ans); } return 0;}
0 0
- hdu1195
- HDU1195
- hdu1195
- HDU1195(BFS)
- hdu1195 bfs
- hdu1195 bfs
- hdu1195 Open the Lock
- hdu1195 Open the Lock
- hdu1195 Open the Lock
- HDU1195的双向bfs
- hdu1195 Open the Lock
- hdu1195-Open the Lock
- hdu1195双向广搜
- HDU1195 Open the Lock
- HDU1195 Open the Lock
- hdu1195 Open the Lock
- HDU1195---Open the Lock
- HDU1195 BFS或者DP
- vimperator 常用操作命令一览表(3分钟回顾)
- 链表题目总结
- centos7上mysql的使用
- hdu3555 经典数位dp
- 【NEFU】17-数字三角形
- HDU1195
- umedit在php中的使用及配置
- LeetCode 290. Word Pattern(单词模式)
- 关于Table View的介绍
- Android笔记【5】--PS 练习买咖啡
- mysq不输入密码也能登录的bug&授权&修改密码
- C/C++内存对齐补齐机制
- 反转链表
- TC SRM688(div 2)