bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
#include <queue> #define N 150
using namespace std; struct Nod
{
int x,y,step;
}node[]; int map[N][N];
int mark[N][N];
int prim[]; int cx[]={,,-,};
int cy[]={,,,-}; int cnt=; void getMap(int x,int y,int re)
{
while()
{
int dx,dy;
dx = x + cx[(re+)%];
dy = y + cy[(re+)%];
if(!(dx>=&&dx<N&&dy>=&&dy<N))
{
break;
}
if(map[dx][dy]==)
{
map[dx][dy]= ++cnt;
node[cnt].x = dx;
node[cnt].y = dy;
// getMap(dx,dy,(re+1)%4);
x = dx;
y = dy;
re = (re+)%;
}
else
{
dx = x + cx[re%];
dy = y + cy[re%];
if(!(dx>=&&dx<N&&dy>=&&dy<N))
{
break ;
}
map[dx][dy]= ++cnt;
node[cnt].x = dx;
node[cnt].y = dy;
// getMap(dx,dy,re);
x = dx;
y = dy;
}
}
} int bfs(int x,int y)
{
memset(mark,,sizeof(mark));
queue<Nod> Q;
Nod temp;
temp.x = node[x].x;
temp.y = node[x].y;
temp.step = ;
Q.push(temp);
mark[temp.x][temp.y] = ;
while(!Q.empty())
{
Nod td = Q.front();
Q.pop();
if(td.x==node[y].x&&td.y==node[y].y)
{
return td.step;
}
int i;
for(i=;i<;i++)
{
temp.x = td.x + cx[i];
temp.y = td.y + cy[i];
temp.step = td.step + ;
if(map[temp.x][temp.y]>=&&map[temp.x][temp.y]<=&&prim[map[temp.x][temp.y]]&&!mark[temp.x][temp.y])
{
mark[temp.x][temp.y]=;
Q.push(temp);
}
}
}
return -;
} int main()
{
cnt=;
int cas=;
map[][]=;
getMap(,,);
node[].x = ;
node[].y = ;
// cout<<cnt<<endl;
int i,j;
prim[]=;
for(i=;i<;i++)
{
for(j=;j*j<=i;j++)
{
if(i%j==)
{
prim[i]=;
break;
}
}
}
// for(i=1;i<=100;i++)
// if(!prim[i])cout<<i<<" "; int x,y;
while(~scanf("%d%d",&x,&y))
{
printf("Case %d: ",cas++);
int ans = bfs(x,y);
if(ans==-)
{
puts("impossible");
}
else
{
printf("%d\n",ans);
}
} return ;
}
bnuoj 25662 A Famous Grid (构图+BFS)的更多相关文章
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
- HDU-4255
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [LeetCode] 827. Making A Large Island
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...
- Unity 2018 Artificial Intelligence Cookbook Second Edition (Jorge Palacios 著)
https://github.com/PacktPublishing/Unity-2018-Artificial-Intelligence-Cookbook-Second-Edition 1 Beha ...
- Robots on a grid(DP+bfs())
链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
随机推荐
- JavaScript--DOM基础(19)
// DOM(Document Object Model)即文档对象模型,针对HTML和XML文档的API(应用程序接口); // DOM描绘了一个层次化的节点树,运行开发人员可以添加/移除和修改页面 ...
- asp.net中用正则表达式验证数据格式
这是一个验证是否为数字的例子: Regex reg = new Regex(@"^[0-9]+\.{0,1}[0-9]*$");reg.IsMatch(textBox1.T ...
- Java JPushV3服务端
因为JPush的官方文档太乱,所以依据原理自行实现. 主要技术就是post数据到https上和https的auth,实现起来还是很容易的. http://pan.baidu.com/s/1sjEc74 ...
- 十六、Android 滑动效果汇总
Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...
- SQL数据库开发知识总结:基础篇
1数据库概述 (1) 用自定义文件格式保存数据的劣势. (2) DBMS(DataBase Management System,数据库管理系统)和数据库,平时谈到”数据库”可能有两种含义:MSSQL ...
- js如何检测打开窗口是否存在的三个方法?
js打开窗口一般也就是使用window.open方法: win = window.open(CHILD_WINDOW_URL, CHILD_WINDOW_NAME, CHILD_WINDOW_ATTR ...
- C#反射技术的简单操作(读取和设置类的属性)
public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type ...
- 20150226—C# winform中的ListView解析
ListView在WinForm中多用于表的构建,可以直观的显示表的信息,其格式如同SQL的表 这是他的位置,在公共控件中: Listview的几个重要属性:Columms(集合).Groups(集合 ...
- tomcat中的get、post区别
最近做一个项目,前台传到后台的数据是乱码.看着代码应该是正确的,但是就是有问题,然后请教了旁边的老司机才找到问题是什么.话不多说,下面是模拟情景的代码,其实也很简单,前台一个form表单,post ...
- Codevs 3231 小吃
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 这里简直是吃货的天堂,小吃太多了.数不胜数. 假设岛上有N种小吃,每种体积Vi, ...