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 ...
随机推荐
- Android Virtual Devices代理上网
本机电脑是使用代理上网,然后要在avd中要连接互联网,设置步骤如下: Click on Menu Click on Settings Click on Wireless & Networks ...
- 合并多个List<T>类型并通过LINQ按指定属性排序
后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...
- 代码快捷键的设置读取App.config方法
附件下载:http://files.cnblogs.com/files/qtiger/ShortcutAchieve.zip 代码实现最重要(增加引用using System.Configuratio ...
- paypal api 相关资料
https://developer.paypal.com/ https://developer.paypal.com/docs/classic/api/merchant/GetBalance_API_ ...
- 第二十章 数据访问(In .net4.5) 之 使用LINQ
1. 概述 .net3.5中新添加给C#的LINQ查询,提供了直观便捷的数据查询方式.并且支持多种数据源的查询. 本章介绍标准的LINQ操作,如何用最优的方式使用LINQ 以及 LINQ to XML ...
- SublimeText插件Emmet的自定义模板
在前端界,作为快速生成代码的Emmet插件相当给力.最近在学bootstrap,需要频繁生成html头文件,我就想着自定义模板.国内只有基础教程,只好自己读英文文档了. Emmet国内基础教程地址: ...
- [php]php数组函数的相关
1.8.26---list与extract的作用区别extractextract -- 从数组中将变量导入到当前的符号表 说明int extract ( array var_array [, int ...
- 分享我常用的一些JS验证和函数
下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用//浮点数除法运算 function fdiv(a, b, n) { if (n == undefi ...
- bat文件编写(无线承载网络设置)
就弄个例子,自己看执行效果,然后模仿写就行. 1)获取当前时间: @echo off set YEAR=%date:~0,4% set MONTH=%date:~5,2% set DAY=%date: ...
- hdu 1496 Equations
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...