1. 程式人生 > >【poj 2488】A Knight's Journey 中文題意&題解&程式碼(C++)

【poj 2488】A Knight's Journey 中文題意&題解&程式碼(C++)

中文題意:
給出一個p行q列的國際棋盤,馬可以從任意一個格子開始走,問馬能否不重複的走完所有的棋盤。如果可以,輸出按字典序排列最小的路徑。列印路徑時,列用大寫字母表示(A表示第一列),行用阿拉伯數字表示(從1開始),先輸出列,再輸出行。

題解:
基礎dfs進行暴力,開一個數組沿途記錄路徑,注意因為要求字典序最小,所以每次都從 A1 這個位置開始搜,且調整好每次搜尋的方向以保證其字典序最小,注意輸出格式。
程式碼:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std; int T,p,q,vis[30][30],flag; int f[2][8]={{-2,-2,-1,-1, 1,1, 2,2}, {-1, 1,-2, 2,-2,2,-1,1}}; struct node{ int x;int y; }ans[30]; void dfs(int x,int y,int step,int tt) { if (flag) return ; vis[x][y]=1; ans[step].x=x; ans[step].y=y; if (step==p*q && flag==0
) { flag=1; printf("Scenario #%d:\n",tt); for (int i=1;i<=step;i++) { char c=ans[i].x-1+'A'; cout<<c<<ans[i].y; } printf("\n"); printf("\n"); return ; } for (int i=0;i<8;i++) { int
nex=x+f[0][i]; int ney=y+f[1][i]; if (nex>=1 && nex<=q && ney>=1 && ney<=p && vis[nex][ney]!=1) dfs(nex,ney,step+1,tt); } vis[x][y]=0; } int main() { scanf("%d",&T); for (int t=1;t<=T;t++) { memset(vis,0,sizeof(vis)); flag=0; scanf("%d%d",&p,&q); dfs(1,1,1,t); if (!flag) { printf("Scenario #%d:\n",t); printf("impossible\n"); printf("\n"); } } }

相關推薦

poj 2488A Knight's Journey 中文題意&題解&程式碼C++

中文題意: 給出一個p行q列的國際棋盤,馬可以從任意一個格子開始走,問馬能否不重複的走完所有的棋盤。如果可以,輸出按字典序排列最小的路徑。列印路徑時,列用大寫字母表示(A表示第一列),行用阿拉伯數

POJ 3630Phone List 中文題意&題解&程式碼C++

Phone List Time Limit: 1000MS Memory Limit: 65536K Description Given a list of phone numbers, determine if it is consistent

POJ2406Power Strings 中文題意&題解&程式碼C++

Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we define a*b to be their conca

poj 3126Prime Path 題意&題解&程式碼C++

變換的過程要保證 每次變換出來的數都是一個 四位素數,而且當前這步的變換所得的素數 與 前一步得到的素數 只能有一個位不同,而且每步得到的素數都不能重複。 求從a到b最少需要的變換次數。無

poj 2627 Sudoku 題意&題解&程式碼C++

題目連結: http://poj.org/problem?id=2676 題意: 給出一個未填的數獨,求這個數獨的解並輸出填好的數獨,若此數獨無解,則輸出原給定的錯誤數獨。 題解: dfs回溯

poj 1159Palindrome 題意&題解&程式碼C++

題目連結: http://poj.org/problem?id=1159 題意: 給出一個長為n的字串,求最少新增幾個字元可以將這個字串變為迴文字串。 題解: 要把這個串變為迴文串,很容易想到

[POJ] 2488.A Knight's Journey

題目傳送門 題意:日字走,一次走完給定的p*q棋盤,記錄步驟,輸出 思路:dfs + 回溯 #include <algorithm> #include <bitset> #include <cstdio> #include <

poj-2488 A Knight's Journey

Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a

poj 2488 A Knight's Journey

題目 題意:給出一個國際棋盤的大小 p*q,判斷馬能否不重複的走過所有格,並記錄下其中按字典序排列的第一種路徑。 因為要求字典序輸出最小,所以按下圖是搜尋的次序搜素出來的就是最小的。 初始方向陣列:int f[2][8]={{-2,-2,-1,-1, 1,1, 2,2}

HDU 2488 A Knight's Journey深搜

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27258 Accepted: 9295 Description Background  The knig

POJ - 3320 Jessica's Reading Problem 尺取,雜湊

題幹: Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it.

A Knight's Journey解題報告陳小賓

  Description Background  The knight is getting bored of seeing the same black and white squares again and again and has decided to mak

A Knight's Journey深度搜索

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42686 Accepted: 14506 Description Background The knight

POJ 3476A Game with Colored Balls

write 開始 有一個 每次 復雜 取出 all 如果 字符子串 POJ 3476 首先寫了個treap,然後常數太大tle了。。。 然後想了個極為復雜的方法,是一共7個dsu,3個bit,還有一個set。然後寫了一半就歇菜了。。。 然後看dxm的方法,是這樣做的: 首先

SpringMVC架構SpringMVC入門實例,解析工作原理

rip 業務邏輯層 popu 輸入 implement override article hide -i 上篇博文,我們簡單的介紹了什麽是SpringMVC。這篇博文。我們搭建一個簡單SpringMVC的環境,使用非註解形式實現一個HelloWorld實

問底夏俊:深入站點服務端技術——站點並發的問題

而是 思路 臨時 系統負載 表現 json article 不能 情況 摘要:本文來自擁有十年IT從業經驗、擅長站點架構設計、Web前端技術以及Java企業級開發的夏俊,此文也是《關於大型站點技術演進的思考》系列文章的最新出爐內容。首發於CSDN,各位技術人員不

python-excelSelenium+python自動化之讀取Excel數據xlrd

logs title .html selenium2 ref target targe pos 數據 Selenium2+python自動化之讀取Excel數據(xlrd) 轉載地址:http://www.cnblogs.com/lingzeng86/p/6793398.h

原始碼剖析tornado-memcached-sessions —— Tornado session 支援的實現

新地址:https://github.com/AngryHacker/articles/issues/5#issue-372211594      童鞋,我就知道你是個好學滴好孩子~來吧,讓我們進行最後的探(zuo)索(si)!    

原始碼剖析tornado-memcached-sessions —— Tornado session 支援的實現

     客官您終於回頭了!讓我們本著探(zuo)索(si)精神把 session.py 看完吧...       首先看看需要的庫:       pickle 一個用於序列化反序列化的庫(聽

重大更新DevExpress v18.1新版亮點——Analytics Dashboard篇

使用者介面套包DevExpress v18.1日前正式釋出,本站將以連載的形式為大家介紹各版本新增內容。本文將介紹了DevExpress Analytics Dashboard v18.1 的新功能,快來下載試用新版本!點選下載>> WPF Dashboard Viewer