大模拟:枚举6个方向。检查每一个0是否能移动

Puzzle


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Little Georgie likes puzzles very much. Recently he has found a wooden triangle in the box with old toys. The side of the triangle is n inches long. The triangle is divided into n2unit
triangles with lines drawn on his surface.

The interesting fact about that triangle is that it is not solid - it consists of two parts. Each of the parts is a connected set of unit triangles. Georgie has put his triangle onto
the table and now wonders whether he can separate the parts. He wants to separate them without taking any part of the triangle off the table, just moving the parts by the table surface. The triangle has a small but non-zero thickness, so while being moved
the parts must not intersect.

For example, if the triangle is divided into parts as it is shown on the top picture below, Georgie can separate the parts the way he wants. However in the case displayed on the bottom
picture, he cannot separate the parts without lifting one of them.

Help Georgie to determine whether he can separate the parts moving them by the surface of the table.

<img src="http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F2610%2Fg.gif" <="" img=""> <img src="http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F2610%2Fg2.gif" <="" img="">

Two puzzles corresponding the samples

Input

Input file contains one or more testcases. The first line of each testcase contains n (2 <= n <= 50). Next n lines contain the description of the triangle, i-th of these lines contains
2i - 1 characters, describing unit triangles in the i-th row, from left to right. Character '0' means that the triangle belongs to the first part of the main triangle, '1' means that it belongs to the second one.

Testcase with n = 0 designates the end of the test data, this testcase must not be processed. There is no blank line in the input file.

Output

For each puzzle output the line with its number followed by the line that states whether the parts can be separated. Do not output any blank lines.

Sample Input

6
0
001
00011
0000011
000111111
00111111111
6
0
001
00111
0011011
000000111
00111111111
0

Sample Output

Puzzle 1
Parts can be separated
Puzzle 2
Parts cannot be separated

Author: Andrew Stankevich

Source: Andrew Stankevich's Contest #7

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; int n;
int tu[55][200]; bool inmap(int x,int y)
{
if((x>=1&&x<=n)&&(y>=1&&y<=2*x-1)) return true;
return false;
} bool check_down(int x,int y,int c,int kind)
{
if(kind==1)
{
/// x+1 --- y,y+1,y+2
/// x+2 --- y+1,y+2,y+3
for(int i=0;i<3;i++)
{
int nx=x+1,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
for(int i=1;i<4;i++)
{
int nx=x+2,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
}
else if(kind==0)
{
/// x --- y-1 y+1
/// x+1 --- y+1 y+2 y+3
/// x+2 -- y+2 for(int i=-1;i<=1;i++)
{
int nx=x,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} for(int i=1;i<=3;i++)
{
int nx=x+1,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} if(inmap(x+2,y+2))
{
if(tu[x+2][y+2]!=c) return false;
}
} return true;
} bool check_left(int x,int y,int c,int kind)
{
if(kind==1)
{
/// x -- y-1 y-2 y-3
/// x-1 y-2,y-3,y-4 for(int i=1;i<=3;i++)
{
int nx=x,ny=y-i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} for(int i=2;i<=4;i++)
{
int nx=x-1,ny=y-i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
}
else if(kind==0)
{
/// x -- y-1 y-2
/// x-1 -- y-1 y-2 y-3 y-4 for(int i=1;i<=2;i++)
{
int nx=x,ny=y-i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} for(int i=1;i<=4;i++)
{
int nx=x-1,ny=y-i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
} return true;
} bool check_right(int x,int y,int c,int kind)
{
if(kind==1)
{
/// x -- y+1 y+2 y+3
/// x-1 y y+1 y+2 y+3 for(int i=1;i<=3;i++)
{
int nx=x,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} for(int i=0;i<=3;i++)
{
int nx=x-1,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
} }
else if(kind==0)
{
/// x y+1 y+2
/// x-1 y-1 ... y+3
for(int i=1;i<=2;i++)
{
int nx=x,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
for(int i=-1;i<=3;i++)
{
int nx=x-1,ny=y+i;
if(inmap(nx,ny))
{
if(tu[nx][ny]!=c) return false;
}
}
}
return true;
} bool check_heng()
{
bool flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(inmap(i,j-1)==false) continue;
if(tu[i][j-1]==0) continue;
else flag=false;
}
}
}
if(flag) return true;
flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(inmap(i,j+1)==false) continue;
if(tu[i][j+1]==0) continue;
else flag=false;
}
}
}
if(flag) return true;
return false;
} bool check_shu()
{
bool flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(j%2==1)
{
int nx=i, ny=j+1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else flag=false;
}
else if(j%2==0)
{
int nx=i-1, ny=j-1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else flag=false;
}
}
}
}
if(flag) return true;
flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(j%2==1)
{
int nx=i+1, ny=j+1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else flag=false;
}
else if(j%2==0)
{
int nx=i, ny=j-1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else flag=false;
}
}
}
}
if(flag) return true;
return false;
} bool check_xie()
{
bool flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(j%2==1)
{
int nx=i, ny=j-1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else
{
flag=false;
}
}
else if(j%2==0)
{
int nx=i-1, ny=j-1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else
{
flag=false;
}
}
}
}
}
if(flag) return true;
flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]==0)
{
if(j%2==1)
{
int nx=i+1, ny=j+1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else
{
flag=false;
}
}
else if(j%2==0)
{
int nx=i, ny=j+1;
if(inmap(nx,ny)==false) continue;
if(tu[nx][ny]==0) ;
else
{
flag=false;
}
}
}
}
}
if(flag) return true;
return false;
} char hang[5000]; int main()
{
int cas=1;
while(scanf("%d",&n)!=EOF&&n)
{
printf("Puzzle %d\n",cas++);
int zero=0,one=0;
for(int i=1;i<=n;i++)
{
scanf("%s",hang+1);
for(int j=1;j<=2*i-1;j++)
{
tu[i][j]=hang[j]-'0';
if(tu[i][j]==0) zero++;
else one++;
}
} if(one==0||zero==0)
{
puts("Parts cannot be separated"); continue;
} bool flag=true; if(check_heng()||check_shu()||check_xie()){puts("Parts can be separated"); continue;} for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]!=0) continue;
if(check_down(i,j,0,j%2)) continue;
else flag=false;
}
} if(flag==true)
{
puts("Parts can be separated"); continue;
} flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]!=0) continue;
if(check_left(i,j,0,j%2)) continue;
else flag=false;
}
} if(flag==true)
{
puts("Parts can be separated"); continue;
} flag=true;
for(int i=1;i<=n&&flag;i++)
{
for(int j=1;j<=2*i-1&&flag;j++)
{
if(tu[i][j]!=0) continue;
if(check_right(i,j,0,j%2)) continue;
else flag=false;
}
} if(flag==true)
{
puts("Parts can be separated"); continue;
}
else
{
puts("Parts cannot be separated");
} }
return 0;
}

ZOJ 2610 Puzzle 模拟的更多相关文章

  1. A - Jugs ZOJ - 1005 (模拟)

    题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...

  2. Capture the Flag ZOJ - 3879(模拟题)

    In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...

  3. ZOJ 3019 Puzzle

    解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS 因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可. 反思:一开始 ...

  4. ZOJ 3705 Applications 模拟

    #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include< ...

  5. ZOJ 3652 Maze 模拟,bfs,读题 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842 要注意题目中两点: 1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才 ...

  6. [ZOJ 1009] Enigma (模拟)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1009 题目大意:给你三个转换轮,只有当第一个转换轮转动一圈后第二 ...

  7. ZOJ 1122 Clock(模拟)

    Clock Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a standard 12-hour clock with ...

  8. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  9. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

随机推荐

  1. [Linux] Ubuntu下的文件比较工具--meld

    在ubuntu中需要比较文件的差异,于是安装meld apt-get install meld 安装完后,在/usr/bin/下找到meld,然后发送到桌面上, 或者在命令行执行meld命令 打开后选 ...

  2. 网页采集(通过HtmlAgilityPack+XPath)

    有HtmlAgilityPack这个类库可以更方便地对HTML内容进行分析和提取.因此今天特别学习和实践了一下HtmlAgilityPack和XPath,并作下笔记. 1.下载HtmlAgilityP ...

  3. Qt 事件处理机制 (下篇)

    继续我们上一篇文章继续介绍,Qt 事件处理机制 (上篇) 介绍了Qt框架的事件处理机制:事件的产生.分发.接受和处理,并以视窗系统鼠标点击QWidget为例,对代码进行了剖析,向大家分析了Qt框架如何 ...

  4. kali开启ssh服务,实现win远程登录

    本人问题:想通过windows7中的putty直接ssh到kali系统,而默认情况下,kali系统ssh服务没有开启. 具体按如下操作进行设置: 照以下步骤进行配置和操作: 1.修改sshd_conf ...

  5. 测试markdown 博客功能

    欢迎使用 Cmd - 在线 Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们 ...

  6. [JS Compose] 7. Ensure failsafe combination using monoids

    monoids is a semi-group with a neutral element. A semigroup, it does not have an element to return s ...

  7. Python网络爬虫 - 一个简单的爬虫例子

    下面我们创建一个真正的爬虫例子 爬取我的博客园个人主页首页的推荐文章列表和地址 scrape_home_articles.py from urllib.request import urlopen f ...

  8. 理解GC

    首先看日志内容:  [Full GC 23.32: [ParNew: 2356K->2310K(3251K), 0.000288sec] [Full GC 是名称. [ParNew:是表示收集器 ...

  9. CA证书服务器从2003迁移到2008 R2!

    1.在源 CA 服务器中备份相应的 CA :

  10. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...