UVa 225 Golygons (黄金图型)—短小精悍hhhh
来源:互联网 发布:2017超好听的网络歌曲 编辑:程序博客网 时间:2024/06/09 20:04
题目链接
题意 一个很酷的旅客 准备用自己的方式丈量城市。从0,0点出发走第一步时步长为1 走第二步时步长为2;并且没走完一步要转90°方向再走下一步。还要避开路障。 然后输出能走回0,0的所有方案 以及方案数
分析 DFS 剪枝
#include<bits/stdc++.h>using namespace std;const int v=233;int n,ans;int dx[4]={1,0,0,-1},dy[4]={0,1,-1,-0};int buf[1000][1000];char ch[4]={'e','n','s','w'},arr[1001];int judge(int a,int b){//确保每次90°转向; if(a==0 || a==3) return b!=0 && b!=3; if(a==1 || a==2) return b!=1 && b!=2; return 1; }bool safe(int x,int y,int a,int b,int f){//确保路上没有路障; while(x!=a || y!=b){ x+=dx[f];y+=dy[f]; if(buf[x+v][y+v] >0) return false; } return buf[x+v][y+v]==0;}void DFS(int step,int x,int y,int f){ if(step>n){ if(!x && !y){ans++;puts(arr);} return; } for(int i=0;i<4;i++){ if(judge(f,i) && safe(x,y,x+step*dx[i],y+step*dy[i],i)){ arr[step-1]=ch[i]; buf[x+step*dx[i]+v][y+step*dy[i]+v]=-1; DFS(step+1,x+step*dx[i],y+step*dy[i],i); buf[x+step*dx[i]+v][y+step*dy[i]+v]=0; } }}int main(void){ int t,m,x,y; cin>>t; while(t-- && cin>>n>>m){ ans=0; memset(buf,0,sizeof(buf)); memset(arr,0,sizeof(arr)); while(m--){cin>>x>>y; buf[x+v][y+v]=1;} if(n==7 || n==8 || n==15 || n==16){// 这里是参考了超级帅气的J1nAB1n9 的剪枝方式 DFS(1,0,0,5); } printf("Found %d golygon(s).\n\n",ans); } return 0;}
J1nAB1n9博客传送门->>J1nAB1n9
恩,聪明的J1nAB1n9同学想到了 如果所走的路线可以拼凑成一个矩形的话 即步长之和可以整除4.那么满足这个条件的n才有可能走回原点。
阅读全文
0 0
- UVa 225 Golygons (黄金图型)—短小精悍hhhh
- UVa 225 Golygons (黄金图型)——我有与众不同的剪枝
- uva 225 - Golygons(暴力)
- UVA - 225 Golygons
- UVA 225 Golygons (DFS)
- UVa 225 Golygons
- UVa 225 - Golygons
- UVA 225(p214)----Golygons
- uva 225 Golygons
- UVA 225 - Golygons(dfs回溯)
- UVa 225 - Golygons (回溯 + 剪枝)
- UVA 225 Golygons(DFS + sort)
- UVa 225:Golygons(DFS)
- Golygons UVA
- 225 - Golygons Golygons 暴力
- UVa 225 - Golygons 解题报告(暴力)
- 【uva】225 - Golygons(dfs有坑)
- UVa 225 - Golygons(DFS+回溯)
- springmvc和easyui使用ajax前台后台互传数据,假删除提示警告问题。
- java集合面试题
- 获取一个数组中重复最多的值
- 现代支付系统设计 ——基于微服务的实践
- 知乎开源的图片选择库
- UVa 225 Golygons (黄金图型)—短小精悍hhhh
- IE下按回车自动触发input type=submit的按钮点击事件
- ssh连接vim编辑时习惯性按下Ctrl+s
- React Native 轻松集成统计功能(Android 篇)
- 1、C程序 数组排序之[冒泡排序法]
- Python爬虫基础
- 毕业两个月总结-第一篇博客
- mysql 更改编码格式(以utf8为例)
- 前端数据传到后台的方法