题目链接:https://vjudge.net/problem/POJ-2488
题目大意
多组数据,每组给定一个 p * q 的国际象棋棋盘,问国际象棋中的骑士能否选取一个起点,然后从这个起点能够不重复地走到所有格子,如果有,输出字典序最小的路径。
分析
先说结论,如果这条路存在,那么字典序最小的路径的起点一定是 A1。
这个结论是猜出来的,因为我看很多帖子说的都不合逻辑,我自己写个程序验证了一下,不过 p 和 q 只能枚举到 7,再往后电脑跑不出来了,结果就是,只要以 A1 为起点走不通,那么以其他点为起点也走不通,最后代码提交 AC 也验证了这一点。
PS:有大佬如果会证明,恳请赐教!!!
验证程序如下:
1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 using namespace std; 19 20 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 21 #define Rep(i,n) for (int i = 0; i < (n); ++i) 22 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 23 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 24 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 25 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 26 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 27 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 28 29 #define pr(x) cout << #x << " = " << x << " " 30 #define prln(x) cout << #x << " = " << x << endl 31 32 #define LOWBIT(x) ((x)&(-x)) 33 34 #define ALL(x) x.begin(),x.end() 35 #define INS(x) inserter(x,x.begin()) 36 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end()) 37 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 38 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower); 39 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); 40 41 #define ms0(a) memset(a,0,sizeof(a)) 42 #define msI(a) memset(a,inf,sizeof(a)) 43 #define msM(a) memset(a,-1,sizeof(a)) 44 45 #define MP make_pair 46 #define PB push_back 47 #define ft first 48 #define sd second 49 50 template<typename T1, typename T2> 51 istream &operator>>(istream &in, pair<T1, T2> &p) { 52 in >> p.first >> p.second; 53 return in; 54 } 55 56 template<typename T> 57 istream &operator>>(istream &in, vector<T> &v) { 58 for (auto &x: v) 59 in >> x; 60 return in; 61 } 62 63 template<typename T1, typename T2> 64 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 65 out << "[" << p.first << ", " << p.second << "]" << " "; 66 return out; 67 } 68 69 inline int gc(){ 70 static const int BUF = 1e7; 71 static char buf[BUF], *bg = buf + BUF, *ed = bg; 72 73 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 74 return *bg++; 75 } 76 77 inline int ri(){ 78 int x = 0, f = 1, c = gc(); 79 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 80 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 81 return x*f; 82 } 83 84 template<class T> 85 inline string toString(T x) { 86 ostringstream sout; 87 sout << x; 88 return sout.str(); 89 } 90 91 inline int toInt(string s) { 92 int v; 93 istringstream sin(s); 94 sin >> v; 95 return v; 96 } 97 98 //min <= aim <= max 99 template<typename T> 100 inline bool BETWEEN(const T aim, const T min, const T max) { 101 return min <= aim && aim <= max; 102 } 103 104 typedef long long LL; 105 typedef unsigned long long uLL; 106 typedef pair< double, double > PDD; 107 typedef pair< int, int > PII; 108 typedef pair< int, PII > PIPII; 109 typedef pair< string, int > PSI; 110 typedef pair< int, PSI > PIPSI; 111 typedef set< int > SI; 112 typedef set< PII > SPII; 113 typedef vector< int > VI; 114 typedef vector< double > VD; 115 typedef vector< VI > VVI; 116 typedef vector< SI > VSI; 117 typedef vector< PII > VPII; 118 typedef map< int, int > MII; 119 typedef map< int, string > MIS; 120 typedef map< int, PII > MIPII; 121 typedef map< PII, int > MPIII; 122 typedef map< string, int > MSI; 123 typedef map< string, string > MSS; 124 typedef map< PII, string > MPIIS; 125 typedef map< PII, PII > MPIIPII; 126 typedef multimap< int, int > MMII; 127 typedef multimap< string, int > MMSI; 128 //typedef unordered_map< int, int > uMII; 129 typedef pair< LL, LL > PLL; 130 typedef vector< LL > VL; 131 typedef vector< VL > VVL; 132 typedef priority_queue< int > PQIMax; 133 typedef priority_queue< int, VI, greater< int > > PQIMin; 134 const double EPS = 1e-8; 135 const LL inf = 0x7fffffff; 136 const LL infLL = 0x7fffffffffffffffLL; 137 const LL mod = 1e9 + 7; 138 const int maxN = 4e4 + 7; 139 const LL ONE = 1; 140 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 141 const LL oddBits = 0x5555555555555555; 142 143 int T; 144 int p, q; 145 string ans; 146 int board[27][27]; 147 int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; 148 int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; 149 150 inline void dfs(int x, int y) { 151 ans.PB('A' + x - 1); 152 ans.PB(y + '0'); 153 board[x][y] = 1; 154 155 Rep(i, 8) { 156 int newx = x + dx[i], newy = y + dy[i]; 157 if(BETWEEN(newx, 1, p) && BETWEEN(newy, 1, q) && !board[newx][newy]) { 158 dfs(newx, newy); 159 if(ans.size() == q * p * 2) break; 160 board[newx][newy] = 0; 161 ans.resize(ans.size() - 2); 162 } 163 } 164 } 165 166 int main(){ 167 //freopen("MyOutput.txt","w",stdout); 168 //freopen("input.txt","r",stdin); 169 //INIT(); 170 For(i, 1, 7) { 171 For(j, 1, 7) { 172 p = i; 173 q = j; 174 ans = ""; 175 ms0(board); 176 dfs(1, 1); 177 178 if(ans.size() == q * p * 2) continue; 179 180 // 暴力枚举其他起点 181 For(a, 1, (p + 1) >> 1) { 182 For(b, 1, (q + 1) >> 1) { 183 if(a == 1 && b == 1) continue; 184 ans = ""; 185 ms0(board); 186 dfs(a, b); 187 if(ans.size() == q * p * 2) { 188 printf("Find other road! "); 189 printf("Scenario #%d, p = %d, q = %d: ", (i - 1) * 100 + j, p, q); 190 cout << ans << endl << endl; 191 return 0; 192 } 193 } 194 } 195 } 196 } 197 return 0; 198 }
代码如下
1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 using namespace std; 19 20 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 21 #define Rep(i,n) for (int i = 0; i < (n); ++i) 22 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 23 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 24 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 25 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 26 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 27 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 28 29 #define pr(x) cout << #x << " = " << x << " " 30 #define prln(x) cout << #x << " = " << x << endl 31 32 #define LOWBIT(x) ((x)&(-x)) 33 34 #define ALL(x) x.begin(),x.end() 35 #define INS(x) inserter(x,x.begin()) 36 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end()) 37 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 38 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower); 39 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); 40 41 #define ms0(a) memset(a,0,sizeof(a)) 42 #define msI(a) memset(a,inf,sizeof(a)) 43 #define msM(a) memset(a,-1,sizeof(a)) 44 45 #define MP make_pair 46 #define PB push_back 47 #define ft first 48 #define sd second 49 50 template<typename T1, typename T2> 51 istream &operator>>(istream &in, pair<T1, T2> &p) { 52 in >> p.first >> p.second; 53 return in; 54 } 55 56 template<typename T> 57 istream &operator>>(istream &in, vector<T> &v) { 58 for (auto &x: v) 59 in >> x; 60 return in; 61 } 62 63 template<typename T1, typename T2> 64 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 65 out << "[" << p.first << ", " << p.second << "]" << " "; 66 return out; 67 } 68 69 inline int gc(){ 70 static const int BUF = 1e7; 71 static char buf[BUF], *bg = buf + BUF, *ed = bg; 72 73 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 74 return *bg++; 75 } 76 77 inline int ri(){ 78 int x = 0, f = 1, c = gc(); 79 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 80 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 81 return x*f; 82 } 83 84 template<class T> 85 inline string toString(T x) { 86 ostringstream sout; 87 sout << x; 88 return sout.str(); 89 } 90 91 inline int toInt(string s) { 92 int v; 93 istringstream sin(s); 94 sin >> v; 95 return v; 96 } 97 98 //min <= aim <= max 99 template<typename T> 100 inline bool BETWEEN(const T aim, const T min, const T max) { 101 return min <= aim && aim <= max; 102 } 103 104 typedef long long LL; 105 typedef unsigned long long uLL; 106 typedef pair< double, double > PDD; 107 typedef pair< int, int > PII; 108 typedef pair< int, PII > PIPII; 109 typedef pair< string, int > PSI; 110 typedef pair< int, PSI > PIPSI; 111 typedef set< int > SI; 112 typedef set< PII > SPII; 113 typedef vector< int > VI; 114 typedef vector< double > VD; 115 typedef vector< VI > VVI; 116 typedef vector< SI > VSI; 117 typedef vector< PII > VPII; 118 typedef map< int, int > MII; 119 typedef map< int, string > MIS; 120 typedef map< int, PII > MIPII; 121 typedef map< PII, int > MPIII; 122 typedef map< string, int > MSI; 123 typedef map< string, string > MSS; 124 typedef map< PII, string > MPIIS; 125 typedef map< PII, PII > MPIIPII; 126 typedef multimap< int, int > MMII; 127 typedef multimap< string, int > MMSI; 128 //typedef unordered_map< int, int > uMII; 129 typedef pair< LL, LL > PLL; 130 typedef vector< LL > VL; 131 typedef vector< VL > VVL; 132 typedef priority_queue< int > PQIMax; 133 typedef priority_queue< int, VI, greater< int > > PQIMin; 134 const double EPS = 1e-8; 135 const LL inf = 0x7fffffff; 136 const LL infLL = 0x7fffffffffffffffLL; 137 const LL mod = 1e9 + 7; 138 const int maxN = 4e4 + 7; 139 const LL ONE = 1; 140 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 141 const LL oddBits = 0x5555555555555555; 142 143 int T; 144 int p, q; 145 string ans; 146 int board[27][27]; 147 int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; 148 int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; 149 150 void dfs(int x, int y) { 151 ans.PB('A' + x - 1); 152 ans.PB(y + '0'); 153 board[x][y] = 1; 154 155 Rep(i, 8) { 156 int newx = x + dx[i], newy = y + dy[i]; 157 if(BETWEEN(newx, 1, p) && BETWEEN(newy, 1, q) && !board[newx][newy]) { 158 dfs(newx, newy); 159 if(ans.size() == q * p * 2) break; 160 board[newx][newy] = 0; 161 ans.resize(ans.size() - 2); 162 } 163 } 164 } 165 166 int main(){ 167 //freopen("MyOutput.txt","w",stdout); 168 //freopen("input.txt","r",stdin); 169 //INIT(); 170 cin >> T; 171 For(cases, 1, T) { 172 cin >> q >> p; 173 ans = ""; 174 ms0(board); 175 176 dfs(1, 1); 177 178 printf("Scenario #%d: ", cases); 179 if(ans.size() != q * p * 2) ans = "impossible"; 180 cout << ans << endl; 181 if(cases != T) cout << endl; 182 } 183 return 0; 184 }