1. 程式人生 > >Paper Folding UVA - 177 模擬+思路+找規律

Paper Folding UVA - 177 模擬+思路+找規律

題目:題目連結

思路:1到4是很容易寫出來的,我們先考慮這四種情況的繪製順序

  1:ru

  2:rulu

  3:rululdlu

  4:rululdluldrdldlu

  不難發現,相較於前一行,每一次增加一倍數量,並且增加的這部分前一半和原來正好相反,後一半相同,根據這一性質,預處理出極端情況13的解答字串,然後繪製即可,注意控制行尾空格

AC程式碼:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5
#include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <deque> 11 12 #define FRER() freopen("in.txt", "r", stdin) 13 #define FREW() freopen("out.txt", "w", stdout) 14 15 #define INF 0x3f3f3f3f 16 17
using namespace std; 18 19 char ans[10000], _map[20000][20000]; 20 int _max[20000]; 21 22 char cal(char ch) { 23 if(ch == 'l') 24 return 'r'; 25 if(ch == 'r') 26 return 'l'; 27 if(ch == 'u') 28 return 'd'; 29 if(ch == 'd') 30 return 'u'; 31 } 32 33 int
main() 34 { 35 //FRER(); 36 //FREW(); 37 ios::sync_with_stdio(0); 38 cin.tie(0); 39 40 ans[0] = 'r'; 41 ans[1] = 'u'; 42 for(int i = 2; i < 14; ++i) { 43 int n = 1 << (i - 2), m = 1 << (i - 1); 44 for(int j = 0; j < n; ++j) { 45 ans[j + m] = cal(ans[j]); 46 } 47 for(int j = 0; j < 1 << (i - 2); ++j) { 48 ans[j + m + n] = ans[j + n]; 49 } 50 } 51 ans[1 << 13] = '\0'; 52 cout << ans << endl; 53 int n; 54 while(cin >> n, n) { 55 memset(_map, 0, sizeof(_map)); 56 memset(_max, 0, sizeof(_max)); 57 int minl = 10000, maxl = 10000, minc = 10000, maxc = 10000; 58 int l = 10000, c = 10000; 59 int m = 1 << n; 60 _max[10000] = 10000; 61 _map[l][c] = '_'; 62 for(int i = 1; i < m; ++i) { 63 if(ans[i - 1] == 'l') 64 --c; 65 else if(ans[i - 1] == 'r') 66 ++c; 67 else if(ans[i - 1] == 'u') 68 --l; 69 70 if(ans[i] == 'u') { 71 72 _map[l][c] = '|'; 73 } 74 else if(ans[i] == 'd') { 75 ++l; 76 _map[l][c] = '|'; 77 } 78 else if(ans[i] == 'l') { 79 --c; 80 _map[l][c] = '_'; 81 } 82 else { 83 ++c; 84 _map[l][c] = '_'; 85 } 86 minl = min(minl, l); 87 minc = min(minc, c); 88 maxl = max(maxl, l); 89 _max[l] = max(_max[l], c); 90 } 91 for(int i = minl; i <= maxl; ++i) { 92 for(int j = minc; j <= _max[i]; ++j) 93 cout << (_map[i][j] == 0 ? ' ' : _map[i][j]); 94 cout << endl; 95 } 96 cout << '^' << endl; 97 } 98 return 0; 99 }