poj 2488 深搜
来源:互联网 发布:绕x轴顺时针旋转矩阵 编辑:程序博客网 时间:2024/05/21 08:58
链接http://poj.org/problem?id=2488
题意:按照字典序遍历整个棋盘,(规定了遍历的策略)因此不能使用广搜,必须使用深搜
ac code
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <math.h>#include <queue>#include <stack>#include <map>#define INF 123123123#define MAX_INDEX 26using namespace std;struct node{ char A; int n;};node result[MAX_INDEX*MAX_INDEX];int result_index = 0;int direction[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};int visit[MAX_INDEX][MAX_INDEX];int p,q,already;int dfs(int x,int y);void debug_visit();int debug_times;int main(){ int n; cin >> n; for(int kk = 1; kk<=n ; kk++) { debug_times = 0; for(int i=0; i<MAX_INDEX; i++) for(int j=0; j<MAX_INDEX; j++) visit[i][j] = 0; cin >> q >> p; already = 0; result_index = 0; int res = dfs(0,0); printf("Scenario #%d:\n",kk); if(res) { for(int i=0; i<result_index; i++) printf("%c%d",result[i].A,result[i].n+1); printf("\n\n"); } else { printf("impossible\n\n"); } } return 0;}int dfs(int x,int y){ if(!visit[x][y]) { visit[x][y] = 1; result[result_index].A = x + 'A'; result[result_index].n = y; result_index++; //debug_visit(); if(result_index == p*q ) return 1; for(int i=0; i<8; i++) { if((x+direction[i][0]>=0)&&(x+direction[i][0]<p)&&(y+direction[i][1]>=0)&&(y+direction[i][1]<q)&&(!visit[x+direction[i][0]][y+direction[i][1]])) { if(dfs(x+direction[i][0],y+direction[i][1])) return 1; } } visit[x][y] = 0; result_index--; return 0; } return 0;}void debug_visit(){ printf("trytimes = %d\n",debug_times++); for(int i=0; i<p; i++) { for(int j=0; j<q; j++) printf("%d ",visit[i][j]); printf("\n"); }}
0 0
- poj 2488 深搜+回溯
- poj 2488 深搜
- poj 2488 深搜
- POJ 2488
- POJ 2488
- POJ 2488
- poj 2488
- poj 2488
- POJ 2488
- poj 2488
- poj 2488
- POJ 2488
- poj 2488
- poj.2488
- POJ 2488
- POJ 2488
- poj-2488
- poj 2488
- vim
- C语言之分支结构
- CentOS安装及配置hadoop
- 机房收费系统 窗体归类
- 2.DHT11数字温湿度传感器的应用
- poj 2488 深搜
- 1008. 数组元素循环右移问题 (20)
- (1.4.6.2)中位数
- VS2013跟友元有仇
- React实践笔记-Quick Start
- 刷一题Leetcode:Ugly Number
- Swift开发 可选类型Optional
- 使用<input type="file">实现文件上传
- Js中的this和window.event.srcElement