URAL 1920 Titan Ruins: the Infinite Power of Magic
大意:
有一张N*N的网格,你每次可以走一步,每格只能走一次,有没有一种方法让走了L步后回到一个距原点1步远的格子?
没有输出Unsuitable device,否则输出Overwhelming power of magic并输出方案。
一开始用DFS 奇偶剪枝了还是TLE,
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#define N 110
using namespace std; int n,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N];
int run[10010][2];
int tem;
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void DFS(int i,int j,int c)
{
if(flag){ return ;}
if(c>t) return ;
if(i<=0||i>n||j<=0||j>n) {return ;}
if(map[i][j]=='D'&&c==t) {flag=ans=true; return ;}
int temp=abs(i-end_i)+abs(j-end_j);
temp=t-temp-c; //t扣掉还要走的最短步temp 和 已经走过的 c 如果这些步还是奇数直接不满足
if(temp&1) return ;//奇偶剪枝 奇数return
for(int k=0;k<4;k++)
if(!visited[i+a[k][0]][j+a[k][1]]) //开始进行各个方向的探索 记得回溯,取消之前走的状态
{
visited[i+a[k][0]][j+a[k][1]]=true;
DFS(i+a[k][0],j+a[k][1],c+1);
if(flag){cout<<i+a[k][0]<<" "<<j+a[k][1]<<endl;break;}
visited[i+a[k][0]][j+a[k][1]]=false;
}
} int main()
{
int i,j;
while(cin>>n>>t)
{
if(t%2!=0||t>n*n){cout<<"Unsuitable device"<<endl;continue;}
else cout<<"Overwhelming power of magic"<<endl;
cout<<"1 1"<<endl;
memset(visited,0,sizeof(visited)); for(i=1;i<=n;i++)
{ for(j=1;j<=n;j++)
{
map[i][j]='.';
}
}
end_i=2;end_j=1;
map[2][1]='D';
visited[1][1]=1; ans=flag=false; DFS(1,1,1);
// if(ans) cout<<"YES"<<endl;
// else cout<<"NO"<<endl;
}
return 0;
}
后来是找规律做出来的,根据奇偶分类讨论一下;;
代码写的很烂。。
很多复用的没复用。。
#include<iostream>
#include<cstring>
#include<cmath>
#define N 110
using namespace std; int n,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N];
int run[10010][2];
int tem;
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int main()
{
int i;int xx,yy,kk;
while(cin>>n>>t)
{
if(t%2!=0||t>n*n){cout<<"Unsuitable device"<<endl;continue;}
else cout<<"Overwhelming power of magic"<<endl;
// cout<<"1 1"<<endl;
if(n%2==0){
if(t<=2*n){
for(i=1;i<=t/2;i++){
cout<<1<<" "<<i<<endl;
}
for(i=t/2;i>0;i--){
cout<<2<<" "<<i<<endl;
}
}
else{
kk=t-2*n;
yy=kk/(2*(n-2));
xx=kk%(2*(n-2));
for(i=1;i<=n;i++){
cout<<"1 "<<i<<endl;
}
if(xx!=0){
int w2=n-2;
for( i=2;i<=2+xx/2;i++){
cout<<i<<" "<<n<<endl;
}
for( i=2+xx/2;i>=2;i--){
cout<<i<<" "<<n-1<<endl;
}
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<w2<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<w2-1<<endl;
w2-=2;
}
while(w2!=0){ cout<<"2 "<<w2<<endl;
cout<<"2 "<<w2-1<<endl;
w2-=2;
}
}
else{
int wei;
wei=n;
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<wei<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<wei-1<<endl;
wei-=2;
}
while(wei!=0){ cout<<"2 "<<wei<<endl;
cout<<"2 "<<wei-1<<endl;
wei-=2;
}
}
}
}
else{ if(t<=2*n){
for(i=1;i<=t/2;i++){
cout<<1<<" "<<i<<endl;
}
for(i=t/2;i>0;i--){
cout<<2<<" "<<i<<endl;
}
}
else if(t<=2*n+2*(n-2)){
int er=(t-(2*n))/2;
for(i=1;i<=n;i++){
cout<<1<<" "<<i<<endl;
}
for(i=n;i>=3;i--){
cout<<2<<" "<<i<<endl;
}
for(i=2;i<=2+er;i++){
cout<<i<<" "<<2<<endl;
}
for(i=er+2;i>=2;i--){
cout<<i<<" "<<1<<endl;
} }
else if(t<=(n+n-1+3*(n-2))){
int bb=(t-(2*n+2*(n-2)))/2;
for(i=1;i<=n;i++){
cout<<1<<" "<<i<<endl;
}
for(i=n;i>=3;i--){
cout<<2<<" "<<i<<endl;
}
for(i=3;i<=n;i++){
cout<<i<<" "<<3<<endl;
}
cout<<n<<" "<<2<<endl;
int x=n,y=1;
cout<<x<<" "<<y<<endl;
x--;
cout<<x<<" "<<y<<endl; while(1){
if(x==2&&y==1){break;}
if(bb>0&&y==1&&x%2==0){
y=2;bb--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==1){
y=1;cout<<x<<" "<<y<<endl;
}
else if(y==1&&x%2==1){
x--;
cout<<x<<" "<<y<<endl;
}
else if(bb<=0&&y==1&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
}
}
else{
kk=t-(n+n-1+3*(n-2));
yy=kk/(2*(n-2));
xx=kk%(2*(n-2));
for(i=1;i<=n;i++){
cout<<"1 "<<i<<endl;
}
if(xx!=0){
int w21=n-2;
for( i=2;i<=2+xx/2;i++){
cout<<i<<" "<<n<<endl;
}
for( i=2+xx/2;i>=2;i--){
cout<<i<<" "<<n-1<<endl;
}
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<w21<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<w21-1<<endl;
w21-=2;
}
while(w21!=3){ cout<<"2 "<<w21<<endl;
cout<<"2 "<<w21-1<<endl;
w21-=2;
}
}
else{
int wei2;
wei2=n;
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<wei2<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<wei2-1<<endl;
wei2-=2;
}
while(wei2!=3){ cout<<"2 "<<wei2<<endl;
cout<<"2 "<<wei2-1<<endl;
wei2-=2;
}
}
/*
*
*/
for(i=2;i<=n;i++){
cout<<i<<" "<<3<<endl;
}
cout<<n<<" "<<2<<endl;
int x=n,y=1;
cout<<x<<" "<<y<<endl;
x--;
cout<<x<<" "<<y<<endl; while(1){
if(x==2&&y==1){break;}
if(y==1&&x%2==0){
y=2;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==1){
y=1;cout<<x<<" "<<y<<endl;
}
else if(y==1&&x%2==1){
x--;
cout<<x<<" "<<y<<endl;
} } } } }
return 0;
}
有的队也有用DFS过的T T
#include<iostream>
using namespace std;
int n,l,ans;
void dfs(int x,int y,int z){
if(ans==l) return ;
if(x>n||y>n) return ;
if(z==0){
printf("%d %d\n",x,y);
ans+=2;
dfs(x+1,y,0);
printf("%d %d\n",x,y+1);
if(n%2==x%2) dfs(x,3,1);
}
else if(z==1){
printf("%d %d\n",x,y);
ans+=2;
dfs(x,y+1,1);
printf("%d %d\n",x-1,y);
if(x==3&&y%2==0) dfs(x-2,y,2);
}
else if(z==2){
ans+=2;
printf("%d %d\n",x,y);
printf("%d %d\n",x,y-1);
}
}
int main(){
cin>>n>>l;
if(l%2==1||n*n<l){
printf("Unsuitable device\n");
}
else{
printf("Overwhelming power of magic\n");
printf("1 1\n");
printf("2 1\n");
ans=4;
dfs(3,1,0);
printf("2 2\n");
if(ans!=n) dfs(2,3,1);
printf("1 2\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
URAL 1920 Titan Ruins: the Infinite Power of Magic的更多相关文章
- URAL - 1920 Titan Ruins: the Infinite Power of Magic(乱搞)
搞死人的题目,,, 就是在n*n的方格中找路径长度为L的回路. 开始的思路值适合n为偶数的情况,而忽视了奇数的case,所以wa了一次. 然后找奇数case的策略,代码从70多行变成了100多,然后改 ...
- URAL - 1917 Titan Ruins: Deadly Accuracy(水题)
水题一个,代码挫了一下: 题意不好理解. 你去一个洞窟内探险,洞窟内有许多宝石,但都有魔法守护,你需要用魔法将它们打下来. 每个宝石都有自己的防御等级,当你的魔法超过它的防御等级时它就会被你打下来. ...
- URAL 1915 Titan Ruins: Reconstruction of Bygones(思路)
搞这个题差不多是从比赛开始到结束. 从自信慢慢的看题一直到wrong到死. 这个题目可以说成是思路题,以为我们只要明白一点,这道题就成了纯暴力的水题, 那就是当操作数不足栈中数字数目的时候,我们就没有 ...
- 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)
题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- OrientDB入门(1)Getting Started
Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...
- Type curtilage home
This year's National Day coincides with the Mid-Autumn festival, the double false merger about eight ...
- HDU5840(SummerTrainingDay08-B 树链剖分+分块)
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5840 This world need more Zhu 树链剖分+暴力
This world need more Zhu 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5840 Description As we all ...
随机推荐
- Windows Server 2008 R2 实现多用户连接远程桌面
前提 1. 确认自己的计算机开启了远程连接 2. 在远程桌面会话主机配置中将"限制每个用户只能进行一个会话"的勾去掉. 实现方法 1. 需要在角色里面安装远程桌面服务: 2. 在用 ...
- Ubuntu12.04卡死的解决方案
刚开始安装的时候用着还行,不过后来发现用了一会总是会出现卡死的状况 后来看了下ubuntu12.04的内核是3.2,后来把内核升级到3.5发现这种情况不会出现了. 查看内核以及升级内核 uname - ...
- Ubuntu14.04安装GNOME3桌面
以下是安装方法: sudo add-apt-repository ppa:gnome3-team/gnome3 sudo apt-get update sudo apt-get dist-upgrad ...
- 清除VS2012生成的不必要文件
VS2012生成的项目文件中会有一个与解决方案同名的sdf文件,并且比较大,可以删除的,具体方法如下: 英文版步骤如下: Tools->Options->Text Editor->C ...
- 04-树5 Root of AVL Tree
平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...
- 第二章 管理程序流(In .net4.5) 之 管理多线程
1. 概述 本章包括同步资源以及取消长时间任务相关的内容. 2. 主要内容 2.1 同步资源 ① lock关键字实现.会阻塞程序,有可能会导致死锁. ② volatile关键字可以禁用编译优化,用于避 ...
- python2.7爬取豆瓣电影top250并写入到TXT,Excel,MySQL数据库
python2.7爬取豆瓣电影top250并分别写入到TXT,Excel,MySQL数据库 1.任务 爬取豆瓣电影top250 以txt文件保存 以Excel文档保存 将数据录入数据库 2.分析 电影 ...
- html中调用silverlight中的方法
在xaml页面中放置一个textblock控件来绑定数据 <ItemsControl x:Name="cityname"> <Items ...
- python 校招信息爬虫程序
发现一个爬虫程序,正在学习中: https://github.com/lizherui/spider_python
- bzoj 1798 [Ahoi2009]Seq 维护序列seq
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 线段树区间更新: 1. 区间同同时加上一个数 2. 区间同时乘以一个数 #inclu ...