蜜蜂跟學妹很喜歡寫情書,在這資訊發達的時代,他們的情書當然不是傳統的輸信,而是用E-Mail傳送的。但蜜蜂很害羞,他不希望他的情書被駭客攔截,因此他決定把內容加密。他跟學妹發明了一種加密方法,他們要加密的文字只有大寫英文字母以及空白字元,首先將每個字元編號,空白編成0,A編成1,B編成2,…,Z編成26。再將編號轉為二進位後,以下圖的方式填入一個RxC的矩陣中:
1 2 3 4 5 6 7
   | 0 → 0 → 0 → 1        ↓ 0 → 1 → 0  0 ↑    ↓ ↓ 0   0 ← 1  0 ↑      ↓ 1 ← 0 ← 1 ← 0
   | 
 
B = 00010, E = 00101, E = 00101
結尾不夠的部分就直接補0,範例中R=4且C=4,要編碼的字串為“BEE”,產生矩陣後,再一列一列的將數字接起來得到最後的編碼結果,因此“BEE”最後會得到的編碼字串是0001010000101010
$R, C\le 20, 5\times \sum |S|\le R\times C$
首先是輸入,使用getline前要先cin.ignore(),我不太清楚哪裡爛掉,反正遇到輸入卡bug就先ignore(?
然後用一個字串把每個字元的二進位給接起來,並且補0,這樣就能直接用那個字串來填格子。
至於填格子的順序,我還沒想到一個好的迴圈可以處理他。
所以我是觀察到他會是先走右邊,不能的話就往下,不能的話就往左,再來往上…
所以就紀錄目前的方向,當不能走時(超出邊界or走過了)就順時針換個方向試試看。
然後把每行分別輸出就是答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
   | 
 
 
 
 
 
 
 
 
 
  #include <bits/stdc++.h> using namespace std;
  #define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
 
  int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 
  signed main() { EmiliaMyWife 	int t; 	cin >> t; 	auto proc = [&] (int x) { 		string s(5, '0'); 		for(int i = 4; ~i; i--) 			if(x >> i & 1) 				s[4 - i] = '1'; 		return s; 	}; 	 	for(int tc = 1; tc <= t; tc++) { 		cout << tc << ' ';
  		int r, c; 		auto check = [&] (int x, int y) { return x>=0&&x<r&&y>=0&&y<c; }; 		string s; 		cin >> r >> c; 		 		cin.ignore(); 		getline(cin, s); 		 		string owo; 		for(char c : s) { 			if(c == ' ') 				owo += proc(0); 			else 				owo += proc(c - 'a' + 1); 		} 		while(owo.size() < r * c) 			owo.push_back('0'); 		 		vector<string> arr(r, string(c, '0')); 		vector<vector<bool>> vis(r, vector<bool>(c)); 		int x = 0, y = -1, it = 0; 		for(char c : owo) { 			while(!check(x + d[it][0], y + d[it][1]) || vis[x + d[it][0]][y + d[it][1]]) 				it = (it + 1) % 4; 			x += d[it][0]; 			y += d[it][1]; 			arr[x][y] = c; 			vis[x][y] = 1; 		} 		 		for(const auto &w : arr) 			cout << w; 		cout << '\n'; 	} }
 
  |