kuangbin专题一简单搜索总结
来源:互联网 发布:天赋宝宝探险软件 编辑:程序博客网 时间:2024/06/05 20:32
E - Find The Multiple
题意:找一个能整除n的数m,对m的要求是只有0和1组成。n不超过200,m长度不超过100.
思路:很多人都写了一个假算法,就是在unsigned long long 的范围里面找就行了,虽然也A了,但博主想在这里提一下正确的思路。
首先用到了同余定理
即以任意x为起点,它的临界点为y=[x(mod n) * 10(mod n)](mod n)
和z=(y+1) (mod n)
只需要找到一条由1至0的路径,然后输出路径就好了
代码如下:
#include<iostream>#include<queue>using namespace std;void bfs();void dfs(int);void ins();int n;int pr[210];bool V[210];int A[210][2];//利用邻接表存储每个点的临接点int main(){ while(cin>>n && n!=0) { if(n==1) cout<<n<<endl; else { ins();//建图 bfs(); cout<<endl; } } return 0;}void ins(){ for(int i=1;i<=n-1;i++) { int p=( (i%n)*(10%n) ) %n;//同余定理 A[i][0]=p;//乘十 A[i][1]=(p+1)%n;//乘十加一 }}void bfs()//{ queue<int> Q; int i,j,k; for(i=0;i<=n;i++) pr[i]=i; memset(V,0,sizeof(V)); Q.push(1); V[1]=true; while(V[0]==false) { int x=Q.front(); Q.pop(); for(i=0;i<=1;i++) { int y=A[x][i]; if(!V[y]) { pr[y]=x; Q.push(y); V[y]=1; } } } cout<<1;//1为起点 dfs(0);}void dfs(int x)//递归查找路径{ if(x!=1) { dfs(pr[x]); int c=A[pr[x]][0]; if(c==x) printf("0");//乘十 else printf("1");//乘十加一 } return ;}
K - Fire!
思路:一个bfs中也可以有两个不同的物体运动。。
#include<iostream>#include<queue>#include<cstring>#include<stdio.h>using namespace std;char map[2005][2005];struct node{ int x, y; char s; int step;};int n, m;int dir[4][2] = { -1,0,1,0,0,-1,0,1 };bool vis[2005][2005];void solve(){ node person, fire; queue<node>Q; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if (map[i][j] == 'J') { vis[i][j] = 1; person.x = i, person.y = j; person.s = 'J'; person.step = 0; } else if (map[i][j] == 'F') { fire.x = i, fire.y = j; fire.s = 'F'; fire.step = 0; Q.push(fire); } Q.push(person); bool flag = false; int time; while (!Q.empty()) { node u = Q.front();Q.pop(); if (u.s == 'J' &&(u.x == 1 || u.x == n || u.y == 1 || u.y == m)) { flag = true; time = u.step; break; } node newnode; for (int i = 0;i < 4;i++) { newnode.x = u.x + dir[i][0]; newnode.y = u.y + dir[i][1]; newnode.step = u.step + 1; newnode.s = u.s; if (map[newnode.x][newnode.y] == '.') { if (u.s == 'F') { map[newnode.x][newnode.y] = 'F'; Q.push(newnode); } else { if (!vis[newnode.x][newnode.y]) { vis[newnode.x][newnode.y] = 1; Q.push(newnode); } } } } } if (flag) printf("%d\n", time+1); else printf("IMPOSSIBLE\n");}int main(){ int T; scanf("%d", &T); while (T--) { memset(vis, 0, sizeof(vis)); scanf("%d %d", &n, &m); for (int i = 1;i <= n;i++) scanf("%s", map[i] + 1); solve(); }}
阅读全文
0 0
- kuangbin专题一简单搜索总结
- [kuangbin带你飞]专题一 简单搜索 总结
- kuangbin专题一 简单搜索 题解思路
- kuangbin [简单搜索专题]
- [kuangbin带你飞]专题一 简单搜索 刷题总结
- [kuangbin神带你飞]专题一 简单搜索
- kuangbin带你飞 专题一 简单搜索 (题解)
- Fire Game [kuangbin带你飞]专题一 简单搜索
- Kuangbin带你飞专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 D - Fliptile
- [kuangbin带你飞]专题一 简单搜索-J - Fire!
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- [kuangbin带你飞]专题一 简单搜索 A POJ1321
- [kuangbin带你飞]专题一 简单搜索 B POJ2251
- [kuangbin带你飞]专题一 简单搜索 C POJ3278
- [kuangbin带你飞]专题一 简单搜索 D poj3279
- [kuangbin带你飞]专题一 简单搜索 E POJ1426
- [kuangbin带你飞]专题一 简单搜索 F POJ3126
- java直接插入排序算法实例
- 【学习笔记】Windows上安装Python
- Graphical Models Reading Group
- Expert Python Programming, 2nd Edition.pdf 英文原版 免费下载
- 两个有序链表的合并与交集
- kuangbin专题一简单搜索总结
- VUE实例的生命周期与VUE组件的生命周期
- chkconfig工具
- 1005. 继续(3n+1)猜想 (25)
- unity界面和代码分离解决方案
- Qrcode生成二维码(普通篇)之jquery.qrcode.min.js
- Mastering JavaScript.pdf 英文原版 免费下载
- oss阿里云上传文件设置下载文件名
- HIDL概览