Problem UVA225-Golygons

Accept:307  Submit:3646

Time Limit: 3000 mSec

 Problem Description

Imagine a country whose cities have all their streets laid out in a regular grid. Now suppose that a tourist with an obsession for geometry is planning expeditions to several such cities.
Starting each expedition from the central cross-roads of a city, the intersection labelled (0,0), our mathematical visitor wants to set off north, south, east or west, travel one block, and view the sights at the intersection (0,1) after going north, (0,-1) after going south, (1,0) after going east or (-1,0) after going west. Feeling ever more enthused by the regularity of the city, our mathematician would like to walk a longer segment before stopping next, going two blocks. What’s more, our visitor doesn’t want to carry on in the same direction as before, nor wishes to double back, so will make a 90o turn either left or right. The next segment should be three blocks, again followed by a right-angle turn, then four, five, and so on with ever-increasing lengths until finally, at the end of the day, our weary traveller returns to the starting point, (0,0).
The possibly self-intersecting figure described by these geometrical travels is called a golygon.
Unfortunately, our traveller will making these visits in the height of summer when road works will disrupt the stark regularity of the cities’ grids. At some intersections there will be impassable obstructions. Luckily, however, the country’s limited budget means there will never be more than 50 road works blocking the streets of any particular city. In an attempt to gain accountability to its citizens, the city publishes the plans of road works in advance. Our mathematician has obtained a copy of these plans and will ensure that no golygonal trips get mired in molten tar.
Write a program that constructs all possible golygons for a city.

 Input

Since our tourist wants to visit several cities, the input file will begin with a line containing an integer specifying the number of cities to be visited.
For each city there will follow a line containing a positive integer not greater than 20 indicating the length of the longest edge of the golygon. That will be the length of the last edge which returns the traveler to (0,0). Following this on a new line will be an integer from 0 to 50 inclusive which indicates how many intersections are blocked. Then there will be this many pairs of integers, one pair per line, each pair indicating the x and y coordinates of one blockage.

 Output

For each city in the input, construct all possible golygons. Each golygon must be represented by a sequence of characters from the set {n,s,e,w} on a line of its own. Following the list of golygons should be a line indicating how many solutions were found. This line should be formatted as shown in the example output. A blank line should appear following the output for each city.
Note: See on the right the diagram of the 1st City

 Sample Input

2 8 2 -2 0 6 -2 8 2 2 1 -2 0
 

 Sample Ouput

wsenenws

Found 1 golygon(s).

Found 0 golygon(s).

题解:很明显的dfs搜索,剪枝也很粗暴,如果剩下的步数不足以回到原点就返回。由于坐标可能是负的,因此原点坐标要取成一个足够大的正值。

坑点:题目中没有说一个城市只能参观一次,但是实际上只能参观一次,需要vis数组。

 #include <bits/stdc++.h>

 using namespace std;

 const int o = ,Max = ;

 int n,ans;
int gra[Max][Max];
int dx[] = {,,,-};
int dy[] = {,,-,};
bool vis[Max][Max];
char Direction[] = {'e','n','s','w'};
int sta[],num[] = {,}; //dir为0代表水平方向,为1代表垂直方向 void dfs(int x,int y,int pos,int dir){
//printf("pos:%d x:%d y:%d\n",pos,x,y);
if(pos == n+){
if(x==o && y==o){
ans++;
printf("%c",Direction[sta[]]);
for(int i = ;i <= n;i++){
printf("%c",Direction[sta[i]]);
}
printf("\n");
}
return;
} int step = ;
for(int i = pos;i <= n;i++) step += i;
if(abs(x-o)+abs(y-o) > step) return; if(dir == -){
for(int i = ;i < ;i++){
int xx = x+pos*dx[i],yy = y+pos*dy[i];
if(vis[xx][yy]) continue;
int k;
if(<=i && i<){
for(k = min(y,yy);k <= max(y,yy);k++){
if(gra[xx][k]) break;
}
if(k == max(y,yy)+){
sta[pos] = i;
vis[xx][yy] = true;
dfs(xx,yy,pos+,);
vis[xx][yy] = false;
}
}
else{
for(k = min(x,xx);k <= max(x,xx);k++){
if(gra[k][yy]) break;
}
if(k == max(x,xx)+){
sta[pos] = i;
vis[xx][yy] = true;
dfs(xx,yy,pos+,);
vis[xx][yy] = false;
}
}
}
}
else{
if(dir==){
for(int i = ;i < ;i++){
int xx = x+pos*dx[i],yy = y+pos*dy[i];
if(vis[xx][yy]) continue;
int k;
for(k = min(y,yy);k <= max(y,yy);k++){
if(gra[xx][k]) break;
}
if(k == max(y,yy)+){
sta[pos] = i;
vis[xx][yy] = true;
dfs(xx,yy,pos+,);
vis[xx][yy] = false;
}
}
}
else if(dir==){
for(int j = ;j < ;j++){
int i = num[j];
int xx = x+pos*dx[i],yy = y+pos*dy[i];
if(vis[xx][yy]) continue;
int k;
for(k = min(x,xx);k <= max(x,xx);k++){
if(gra[k][yy]) break;
}
if(k == max(x,xx)+){
sta[pos] = i;
vis[xx][yy] = true;
dfs(xx,yy,pos+,);
vis[xx][yy] = false;
}
}
}
}
} int main()
{
#ifdef GEH
freopen("helloworld.01.inp","r",stdin);
#endif
int iCase;
scanf("%d",&iCase);
while(iCase--){
ans = ;
memset(vis,false,sizeof(vis));
memset(gra,,sizeof(gra));
//vis[o][o] = true;
int k,x,y;
scanf("%d%d",&n,&k);
for(int i = ;i < k;i++){
scanf("%d%d",&x,&y);
if(abs(x)>o || abs(y)>o) continue;
gra[o+x][o+y] = ;
}
dfs(o,o,,-);
printf("Found %d golygon(s).\n\n",ans);
}
return ;
}

UVA225-Golygons(dfs)的更多相关文章

  1. UVA225 Golygons 黄金图形(dfs+回溯)

    剪枝1:在同一个维度上的点具有相同的奇偶性,如果奇数数量只有奇数个那么一定不能返回原点. 剪枝2:当前位置怎么也走不回去. 3:沿途判断障碍即可. 在oj上提交0.347s,最快的0.012s,应该有 ...

  2. UVa225,Golygons

    刘儒家翻译的走出的图形可以自交,不知道大家是怎么理解的,反正我是认为这句话的意思是告诉我允许一个点访问多次 这样是WA的,n=15和n=16时多输出很多数据,应该是不允许自交,也就是不允许一个点访问多 ...

  3. 【习题 7-2 UVA-225】Golygons

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力枚举每次走哪里就好. 用一个二维数组来判重.(数据里,要求不能经过一个点两次->但路径可以相交 然后再用一个flag数组, ...

  4. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  5. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  6. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  9. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

随机推荐

  1. java通过Access_JDBC30读取access数据库时无法获取最新插入的记录

    1.编写了一个循环程序,每几秒钟读取一次,数据库中最新一行数据 连接access数据库的方法和查询的信息.之后开一个定时去掉用. package javacommon.util; import jav ...

  2. Maven(六)Eclipse使用Maven插件创建项目

    1. 创建Maven版Java工程 1.1 具体步骤 1.2 更改默认JDK版本 默认JDK版本过低 可以通过配置setting.xml来更改JDK版本 加入如下代码 <profile> ...

  3. OOP设计模式在路上(一)——简单工厂模式

    前言 目前以LabVIEW为主要开发工具,熟悉常规开发框架(队列+状态机),个人用得比较多也感觉比较好用和强大的(JKI,AMC),也用它们开发过一些测试平台,但感觉到了一个瓶颈期,想寻求突破,提升L ...

  4. 《JavaScript高级程序设计》笔记:引用类型(五)

    Object类型 创建object实例方法有两种.第一种方法使用new操作符后跟object构造函数.如下: var person=new Object(); person.name="Ni ...

  5. html之多行文本textarea 及下拉框select(12)

    1.多行文本 多行文本使用textarea标签,默认值需要写在中间,和input标签不同,name属性用于后台获取数据(request.POST.get(meno)) <body> < ...

  6. 用ABP只要加人即可马上加快项目进展(二) - 分工篇

    2018年和1998年其中两大区别就是: 前端蓬勃发展, 前后端分离是一个十分大的趋势. 专门的测试人员角色被取消, 多出了一个很重要的角色, 产品经理   ABP只要加入即可马上加快项目进展, 选择 ...

  7. Linux 安装 jdk8

    切换目录 cd /usr 创建目录 mkdir java cd java 下载 jdk rz 或者 ftp 都行,只要能成功上传 解压 tar zxvf jdk-8u181-linux-x64.tar ...

  8. Python 对服务器返回数据编码进行判断之chardet

    对服务器返回数据编码进行判断之chardet by:授客 QQ:1033553122   测试环境 Win764Bit   chardet-2.3.0 下载地址1:https://pypi.pytho ...

  9. Android项目实战(三十三):AS下获取获取依赖三方的jar文件、aar 转 jar

    使用 Android studio 开发项目中,有几种引用三方代码的方式:jar 包 ,类库 ,gradle.build 的compile依赖. 大家会发现github上不少的项目只提供compile ...

  10. vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed for prop "value".报错解决

    在uni中使用 picker组件,一直报错 vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed ...