109. Magic of David Copperfield II 构造 难度:2
109. Magic of David Copperfield II
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
The well-known magician David Copperfield loves lo show the following trick: a square with N rows and N columns of different pictures appears on a TV screen, Let us number all the pictures in the following order:
| 1 | 2 | ... | N |
| ... | ... | ... | ... |
| N*(N-1)+1 | N*(N-1)+2 | ... | N*N |
Each member of the audience is asked to put a finger on the upper left picture (i.e., picture number one) and The Magic begins: the magician tells the audience to move the finger K1 times through the pictures (each move is a shift of the finger to the adjacent picture up, down, left or right provided that there is a picture to move to), then with a slight movement of his hand he removes some of the pictures with an exclamation "You are not there!", and ... it is true - your finger is not pointing to any of the pictures removed. Then again, he tells the audience to make K2 moves, and so on. At the end he removes all the pictures but one and smiling triumphantly declares, "I've caught you" (applause).
Just now, David is trying to repeat this trick. Unfortunately, he had-a hard day before, and you know how hard to conjure with a headache. You have to write a program that will help David to make his trick.
Input
The input file contains a single integer number N (1<N<101).
Output
Your program should write the following lines with numbers to the output file:
K1 X1,1 X1,2 ... X1,m1
K2 X2,1 X2,2 ... X2,m2
...
Ke Xe,1 Xe,2 ... Xe,me
where Ki is a number of moves the audience should make on the i-th turn (N<=Ki<300). All Ki, should be different (i.e. Ki<>Kj when i<>j). Xi,1 Xi,2 ... Xi,mi are the numbers of the pictures David should remove after the audience will make Ki moves (the number of the pictures removed is arbitrary, but each picture should be listed only once, and at least one picture should be removed on each turn).
A description of the every next turn should begin with a new line. All numbers on each line should be separated by one or more spaces. After e iterations, all pictures except one should be removed.
Sample Input
3
Sample Output
3 1 3 7 9
5 2 4 6 8 一开始被题意吓住了,但是画一画就能发现,
只要是奇数步,就不能停留在原位,要是把整个迷宫划分成国际棋盘的黑白格,那么奇数步必然只能转移到异色格上,
注意到这点而且题目是special judge ,只需注意随时保持还没被染色的在一块大联通域里面就行了,(要是被孤立了那么玩家就不能再走,魔术师就算是没有完成魔术,要是多个区块就不知道玩家到底在哪个区块了,这样无法消除到只有一个含一块的区块)
这里为了保证这一点我使用的是从外向里,每次推一层的方法,假设出发点(0,0)是白格(计算中使用了二维坐标,结果转化为i*n+j+1)
对于里面的第i层,先消去上一层i-1层未涂色的白格,再消去i层可以消去的白格(相邻的黑格都有两个及以上个没有被消去的白格相邻),最后新建一个操作,消去i层的所有黑格(这时候i层的白格不会受影响,因为还有i+1层)

大致染色过程如图
因为染色操作最多有(n/2)*2,开头使用n也不会超出300的操作数
W原因:没有注意到ki<300
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=102;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
bool vis[maxn][maxn];
int n;
int heap1[maxn*maxn],len1,heap2[maxn*maxn],len2,heapt[maxn*maxn],lent;//heap1 第i圈可以消去的白 heap2 第i-1圈没有消掉的白 heapt 第i圈的第二个操作消去的黑格
bool oneentry(int x,int y){//只有一个出口
int fl=0;
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<n&&!vis[tx][ty]){
fl++;
}
}
return fl<=1;
}
bool avail(int x,int y){//是否会抓住顾客
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<n&&!vis[tx][ty]){
if(oneentry(tx,ty))return false;
}
}
return true;
}
int main(){
while(scanf("%d",&n)==1){
memset(vis,0,sizeof(vis));
len1=len2=lent=0;
int num=n&1?n:n+1;
for(int i=0;i<n/2;i++){
printf("%d ",num);num+=2;
for(int k=0;k<len2;k++)printf("%d ",heap2[k]);//上层未涂色白格
len1=len2=lent=0;
int x=i,y=i;
for(int dr=0;dr<4;dr++){
for(int j=0;j<n-2*i-1;j++){
if((x+y)&1){heapt[lent++]=x*n+y+1;vis[x][y]=true;}//黑格
else if(avail(x,y)){heap1[len1++]=x*n+y+1;vis[x][y]=true;}//暂时不能选的白格
else {heap2[len2++]=x*n+y+1;vis[x][y]=true;}//可选择白格
x+=dx[dr];
y+=dy[dr];
}
}
for(int k=0;k<len1;k++){
printf("%d%c",heap1[k],k==len1-1?'\n':' ');//涂白格
}
printf("%d ",num);num+=2;
for(int k=0;k<lent;k++)printf("%d%c",heapt[k],k==lent-1?'\n':' ');//涂黑格
}
}
return 0;
}
109. Magic of David Copperfield II 构造 难度:2的更多相关文章
- 构造 - SGU 109 Magic of David Copperfield II
Magic of David Copperfield II Problem's Link Mean: 略 analyse: 若i+j为奇数则称(i,j)为奇格,否则称(i+j)为偶格,显然每一次报数后 ...
- sgu 109 Magic of David Copperfield II
这个题意一开始没弄明白,后来看的题解才知道这道题是怎么回事,这道题要是自己想难度很大…… 你一开始位于(1,1)这个点,你可以走k步,n <= k < 300,由于你是随机的走的, 所以你 ...
- Magic of David Copperfield II(奇偶性)
题目大意:这是一个魔术游戏,首先把你的手指放在一个左上角的格子里面,然后魔术师说你可以移动K1步,移动完之后,他会删除一些方格,并且说,你肯定不在这里,删除的方格不可以再去了,然后让你再走K2步,继续 ...
- UVa LA 4094 WonderTeam 构造 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- BZOJ3098: Hash Killer II(构造)
Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 2162 Solved: 1140[Submit][Status][ ...
- Codeforces 346C Number Transformation II 构造
题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- SGU 138. Games of Chess 构造 难度:2
138. Games of Chess time limit per test: 0.25 sec. memory limit per test: 4096 KB N friends gathered ...
- sgu 137. Funny Strings 线性同余,数论,构造 难度:3
137. Funny Strings time limit per test: 0.25 sec. memory limit per test: 4096 KB Let's consider a st ...
随机推荐
- 在CentOS Linux系统上,添加新的端口,启用ssh服务
SSH作为Linux远程连接重要的方式,如何配置安装linux系统的SSH服务,如何开启SSH? SSH是什么? SSH 为 Secure Shell 由 IETF 的网络工作小组(Network W ...
- P3938 斐波那契
思路 脑子还真的是好东西,自己太笨了 容易发现父亲节点和儿子节点的关系 儿子节点大于父亲节点 儿子节点和父亲节点之差为斐波那契数,且斐波那契数为小于儿子节点的最大的一个 1e12中有60左右的斐波那契 ...
- BZOJ 1044: [HAOI2008]木棍分割 DP 前缀和优化
题目链接 咳咳咳,第一次没大看题解做DP 以前的我应该是这样的 哇咔咔,这tm咋做,不管了,先看个题解,再写代码 终于看懂了,卧槽咋写啊,算了还是抄吧 第一问类似于noip的那个跳房子,随便做 这里重 ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1686 Oulipo(KMP)题解
题意:主串中能找到几个模式串 思路:超详细解释KMP KMP:针对这个代码,解释一下Fail数组的含义:T为主串,P为模式串,Fail代表失配值,即当P[j] != T[i]时,j要指向的位置为Fai ...
- C# “试图访问已释放的资源”
WinCE项目 VS2008 项目现有一个公共静态类PublicItems,一个窗体模板FormModel,三个继承自模板的子窗体. 现在想要实现在其中一个子窗体中对所有子窗体上一个Label显示进行 ...
- python 写文件刷新缓存
搞爬虫的时候,结果是通过file.write(strs)写入文件的. 带来的问题是,进程如果是被杀死的时候,最后一条结果总是缺损的,因为缓存的部分还未写入文件. 解决办法是每次写入文件时,都刷新缓存, ...
- R语言 sub与gsub函数的区别
> text <- c("we are the world", "we are the children") > sub("w&qu ...
- 词云wordcloud类介绍&python制作词云图&词云图乱码问题等小坑
词云图,大家一定见过,大数据时代大家经常见,我们今天就来用python的第三方库wordcloud,来制作一个大数据词云图,同时会降到这个过程中遇到的各种坑, 举个例子,下面是我从自己的微信上抓的微信 ...
- Java东西太多,记录一些知识点
实习两个月了,这两个月接触了不少东西,简单列举一下知识,未来需要多多学习和了解. 1.前端js.extjs4.Jquery(js框架这些基本现学现用): 2.基础不好要补补Servlet和JSP(再往 ...