Week 4 作業檢討
被選中的人
與被選中的人同列(row)同行(col)
row
column

被選中
的人
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...

被選中
的人
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
不在尋找範圍內
要注意
從
中找出最大的數字
所求:
row
column
1. 輸入座位
2. 尋找被選的人在哪一列(row)、哪一行(col)
3. 尋找該列(row)、該行(col)最大的數字
- 宣告二維陣列,並用雙層迴圈輸入資訊
- 用雙層迴圈遍歷全部的座位,直到找到被選的人
- 分別用一層迴圈搜尋
並用變數記住他的row和column
#include<iostream>
int main(){
int m,n;
std::cin >> m >> n;
int seat[10][10];
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
std::cin >> seat[i][j];
}
}
int chosenNumber,row,col;
std::cin >> chosenNumber;
for(int i = 0,find_chosen_number=0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(seat[i][j] != chosenNumber) continue;
row = i;
col = j;
find_chosen_number = 1;
}
if(find_chosen_number) break;
}
int maxNumber = 0;
for(int i = 0 ; i < n ; i++){
if(seat[row][i] > maxNumber && i != col){
maxNumber = seat[row][i];
}
}
for(int i = 0 ; i < m ; i++){
if(seat[i][col] > maxNumber && i != row){
maxNumber = seat[i][col];
}
}
std::cout << maxNumber << std::endl;
return 0;
}
#include<iostream>
int main(){
int m,n;
std::cin >> m >> n;
int seat[10][10];
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
std::cin >> seat[i][j];
}
}
int chosenNumber,row,col;
std::cin >> chosenNumber;
for(int i = 0,find_chosen_number=0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(seat[i][j] != chosenNumber) continue;
row = i;
col = j;
find_chosen_number = 1;
break;
}
if(find_chosen_number) break;
}
int maxNumber = 0;
for(int i = 0 ; i < n ; i++){
if(seat[row][i] > maxNumber && i != col){
maxNumber = seat[row][i];
}
}
for(int i = 0 ; i < m ; i++){
if(seat[i][col] > maxNumber && i != row){
maxNumber = seat[i][col];
}
}
std::cout << maxNumber << std::endl;
return 0;
}
19 : 如果該座位不是我要找的號碼,就跳過該號(continue),跑下一圈 迴圈
19 : 如果該座位是我要找的號碼,就會執行20-23行
row = 該座位的列數,col = 該座位的行數
#include<iostream>
int main(){
int m,n;
std::cin >> m >> n;
int seat[10][10];
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
std::cin >> seat[i][j];
}
}
int chosenNumber,row,col;
std::cin >> chosenNumber;
for(int i = 0,find_chosen_number=0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(seat[i][j] != chosenNumber) continue;
row = i;
col = j;
find_chosen_number = 1;
}
if(find_chosen_number) break;
}
int maxNumber = 0;
for(int i = 0 ; i < n ; i++){
if(seat[row][i] > maxNumber && i != col){
maxNumber = seat[row][i];
}
}
for(int i = 0 ; i < m ; i++){
if(seat[i][col] > maxNumber && i != row){
maxNumber = seat[i][col];
}
}
std::cout << maxNumber << std::endl;
return 0;
}
29-33 : 比較同row不同col的數字
35-39 : 對於不同row同col的數字
AC code
#include<iostream>
int main(){
int m,n;
std::cin >> m >> n;
int seat[10][10];
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
std::cin >> seat[i][j];
}
}
int chosenNumber,row,col;
std::cin >> chosenNumber;
for(int i = 0,find_chosen_number=0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(seat[i][j] != chosenNumber) continue;
row = i;
col = j;
find_chosen_number = 1;
break;
}
if(find_chosen_number) break;
}
int maxNumber = 0;
for(int i = 0 ; i < n ; i++){
if(seat[row][i] > maxNumber && i != col){
maxNumber = seat[row][i];
}
}
for(int i = 0 ; i < m ; i++){
if(seat[i][col] > maxNumber && i != row){
maxNumber = seat[i][col];
}
}
std::cout << maxNumber << std::endl;
return 0;
}
常犯錯誤總結
1. 把chosenNumber列入計算
本題困難的點 :
1. 地圖訊息為長串數字
2. 地圖內部的移動
Ex : 101000101000000000101000101
用迴圈把字元一個一個吃進來 ,稍後介紹兩種寫法
檢查 x、y、z 移動後是否會超過邊界 或 是否會撞牆 ?
則不移動,並輸出Ooops!!!
超過邊界
撞牆
若是移動後會
常犯錯誤

正確的順序
- 座標順序輸錯
xy平面
z
xy平面
z
xy平面
#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z ;
int N = 105;
char graph[N][N][N];
for(int i=0;i<x*y*z;i++){
cin >> graph[i%x][i/x%y][i/(x*y)];
}
// 初始座標
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
int T;
cin >> T;
while(T--){
int command;
cin >> command;
if(command == 1){
if(graph[cur_x+1][cur_y][cur_z] !='1' && cur_x+1 < x) cur_x++;
else cout << "Ooops!!!" << endl;
}
else if(command == 2){
if(graph[cur_x-1][cur_y][cur_z] !='1' && cur_x-1 >=0) cur_x--;
else cout << "Ooops!!!" << endl;
}
else if(command == 3){
if(graph[cur_x][cur_y-1][cur_z] !='1' && cur_y-1 >=0) cur_y--;
else cout << "Ooops!!!" << endl;
}
else if(command == 4){
if(graph[cur_x][cur_y+1][cur_z] !='1' && cur_y+1 < y) cur_y++;
else cout << "Ooops!!!" << endl;
}
else if(command == 5){
if(graph[cur_x][cur_y][cur_z+1] !='1' && cur_z+1 < z) cur_z++;
else cout << "Ooops!!!" << endl;
}
else if(command == 6){
if(graph[cur_x][cur_y][cur_z-1] !='1' && cur_z-1 >= 0) cur_z--;
else cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z;
int N = 105;
char graph[N][N][N];
for(int k = 0 ; k < z ; k++){
for(int j = 0 ; j < y ; j++){
for(int i = 0 ; i < x ; i++){
cin >> graph[i][j][k];
}
}
}
// 初始座標
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
int T;
cin >> T;
while(T--){
int command;
cin >> command;
if(command == 1){
if(graph[cur_x+1][cur_y][cur_z] !='1' && cur_x+1 < x) cur_x++;
else cout << "Ooops!!!" << endl;
}
else if(command == 2){
if(graph[cur_x-1][cur_y][cur_z] !='1' && cur_x-1 >=0) cur_x--;
else cout << "Ooops!!!" << endl;
}
else if(command == 3){
if(graph[cur_x][cur_y-1][cur_z] !='1' && cur_y-1 >=0) cur_y--;
else cout << "Ooops!!!" << endl;
}
else if(command == 4){
if(graph[cur_x][cur_y+1][cur_z] !='1' && cur_y+1 < y) cur_y++;
else cout << "Ooops!!!" << endl;
}
else if(command == 5){
if(graph[cur_x][cur_y][cur_z+1] !='1' && cur_z+1 < z) cur_z++;
else cout << "Ooops!!!" << endl;
}
else if(command == 6){
if(graph[cur_x][cur_y][cur_z-1] !='1' && cur_z-1 >= 0) cur_z--;
else cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
照座標公式輸入
自己觀察座標輸入的規律
用三層迴圈輸入
a.
b.
#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z ;
int N = 105;
char str[1000005],graph[N][N][N];
cin >> str;
for(int i=0;i<x*y*z;i++){
graph[i%x][i/x%y][i/x/y] = str[i];
}
// 初始座標
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
int T;
cin >> T;
while(T--){
int command;
cin >> command;
if(command == 1){
if(graph[cur_x+1][cur_y][cur_z] !='1' && cur_x+1 < x) cur_x++;
else cout << "Ooops!!!" << endl;
}
else if(command == 2){
if(graph[cur_x-1][cur_y][cur_z] !='1' && cur_x-1 >=0) cur_x--;
else cout << "Ooops!!!" << endl;
}
else if(command == 3){
if(graph[cur_x][cur_y-1][cur_z] !='1' && cur_y-1 >=0) cur_y--;
else cout << "Ooops!!!" << endl;
}
else if(command == 4){
if(graph[cur_x][cur_y+1][cur_z] !='1' && cur_y+1 < y) cur_y++;
else cout << "Ooops!!!" << endl;
}
else if(command == 5){
if(graph[cur_x][cur_y][cur_z+1] !='1' && cur_z+1 < z) cur_z++;
else cout << "Ooops!!!" << endl;
}
else if(command == 6){
if(graph[cur_x][cur_y][cur_z-1] !='1' && cur_z-1 >= 0) cur_z--;
else cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
不建議這樣寫
浪費記憶體空間,多宣告了str[1000005]
則不移動,並輸出Ooops!!!
超過邊界
撞牆
若是移動後會
如何判斷 ? 以x方向往前、x方向往後為例

#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z;
int N = 105;
char graph[N][N][N];
for(int k = 0 ; k < z ; k++){
for(int j = 0 ; j < y ; j++){
for(int i = 0 ; i < x ; i++){
cin >> graph[i][j][k];
}
}
}
// 初始座標
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
int T;
cin >> T;
while(T--){
int command;
cin >> command;
if(command == 1){
if(graph[cur_x+1][cur_y][cur_z] !='1' && cur_x+1 < x) cur_x++;
else cout << "Ooops!!!" << endl;
}
else if(command == 2){
if(graph[cur_x-1][cur_y][cur_z] !='1' && cur_x-1 >=0) cur_x--;
else cout << "Ooops!!!" << endl;
}
else if(command == 3){
if(graph[cur_x][cur_y-1][cur_z] !='1' && cur_y-1 >=0) cur_y--;
else cout << "Ooops!!!" << endl;
}
else if(command == 4){
if(graph[cur_x][cur_y+1][cur_z] !='1' && cur_y+1 < y) cur_y++;
else cout << "Ooops!!!" << endl;
}
else if(command == 5){
if(graph[cur_x][cur_y][cur_z+1] !='1' && cur_z+1 < z) cur_z++;
else cout << "Ooops!!!" << endl;
}
else if(command == 6){
if(graph[cur_x][cur_y][cur_z-1] !='1' && cur_z-1 >= 0) cur_z--;
else cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z;
int N = 105;
char graph[N][N][N];
for(int k = 0 ; k < z ; k++){
for(int j = 0 ; j < y ; j++){
for(int i = 0 ; i < x ; i++){
cin >> graph[i][j][k];
}
}
}
// 初始座標
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
int T;
cin >> T;
while(T--){
int command;
cin >> command;
if(command == 1){
if(graph[cur_x+1][cur_y][cur_z] !='1' && cur_x+1 < x) cur_x++;
else cout << "Ooops!!!" << endl;
}
else if(command == 2){
if(graph[cur_x-1][cur_y][cur_z] !='1' && cur_x-1 >= 0) cur_x--;
else cout << "Ooops!!!" << endl;
}
else if(command == 3){
if(graph[cur_x][cur_y-1][cur_z] !='1' && cur_y-1 >= 0) cur_y--;
else cout << "Ooops!!!" << endl;
}
else if(command == 4){
if(graph[cur_x][cur_y+1][cur_z] !='1' && cur_y+1 < y) cur_y++;
else cout << "Ooops!!!" << endl;
}
else if(command == 5){
if(graph[cur_x][cur_y][cur_z+1] !='1' && cur_z+1 < z) cur_z++;
else cout << "Ooops!!!" << endl;
}
else if(command == 6){
if(graph[cur_x][cur_y][cur_z-1] !='1' && cur_z-1 >= 0) cur_z--;
else cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
或是你可以這樣寫
#include<iostream>
using namespace std;
int main(){
int x,y,z;
cin >> x >> y >> z ;
int N = 105;
char graph[N][N][N];
for(int i=0;i<x*y*z;i++){
cin >> graph[i%x][i/x%y][i/x/y];
}
int cur_x,cur_y,cur_z;
cin >> cur_x >> cur_y >> cur_z;
// direction 縮寫 dirc
// 控制走向 {x,y,z}
int dirc[6][3]={ {1,0,0}, // dirc[0] x座標往前
{-1,0,0}, // dirc[1] x座標往後
{0,-1,0}, // dirc[2] y座標往後
{0,1,0}, // dirc[3] y座標往前
{0,0,1}, // dirc[4] z座標往前
{0,0,-1}}; // dirc[5] z座標往後
int T;
cin >> T;
while(T--){
int command;
cin >> command;
command--;
cur_x += dirc[command][0];
cur_y += dirc[command][1];
cur_z += dirc[command][2];
if( cur_x < 0 || cur_x >= x || cur_y < 0 || cur_y >= y || cur_z < 0 || cur_z >= z || graph[cur_x][cur_y][cur_z] == '1'){
cur_x -= dirc[command][0];
cur_y -= dirc[command][1];
cur_z -= dirc[command][2];
cout << "Ooops!!!" << endl;
}
}
cout << cur_x << ' ' << cur_y << ' ' << cur_z << endl;
return 0;
}
常犯錯誤總結
1. 輸入地圖時,座標輸錯
2. 陣列太小導致RE(Run time error)
3. 移動指令沒有和座標對齊
Ex:輸入座標是( x , y , z ) ,但移動指令寫成( y , x , z ) 或其他錯誤形式
4. 沒有檢查移動後是否超過邊界
5. 撞牆沒有輸出 "Ooops!!!" (without quotaiton mark)
Ex:題目座標是( x , y , z ) ,但輸成 ( z , x , y ) 或其他錯誤形式
- 向量、單迴圈寫法
設4個向量 控制(row,col)的走向
- 往右走 (0,1)
- 往下走 (1,0)
- 往左走 (0,-1)
- 往上走 (-1,0)
假設當前座標為 (row,col)

上邊界 up
下邊界 bottom
左
邊
界
left
右
邊
界
right
#include <iostream>
using namespace std;
int const N = 100;
int arr[N][N];
int main()
{
int n;
cin >> n;
//控制方向
int dirc[4][2] = { {0,1}, // 往右走,此時t = 0
{1,0}, // 往下走,此時t = 1
{0,-1}, // 往左走,此時t = 2
{-1,0} }; // 往上走,此時t = 3
int left = 0, up = 0, right = n-1, bottom = n-1;
int num = 1 , t = 0 , row=0 , col=0;
while(num <= n*n){
arr[row][col] = num;
// col一直往右走,直到碰到右邊界(right)。若要超過右邊界了,則向量變為往下走
if(col + dirc[t][1] > right){
t = 1;
up++;
}
// row一直往下走,直到碰到下邊界(bottom)。若要超過下邊界了,則向量變為往左走
else if(row + dirc[t][0] > bottom){
t = 2;
right--;
}
// col一直往左走,直到碰到左邊界(left)。若要超過左邊界了,則向量變為往上走
else if(col + dirc[t][1] < left){
t = 3;
bottom--;
}
// row一直往上走,直到碰到上邊界(up)。若要超過上邊界了,則向量變為往右走
else if(row + dirc[t][0] < up){
t = 0;
left++;
}
row += dirc[t][0];
col += dirc[t][1];
num++;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n ; j++){
if(j > 0) cout << ' ';
cout << arr[i][j];
}
cout << endl;
}
return 0;
}

上邊界 up
下邊界 bottom
左
邊
界
left
右
邊
界
right
#include <iostream>
using namespace std;
int const N = 100;
int arr[N][N];
int main()
{
int n;
cin >> n;
//控制方向
int dirc[4][2] = { {0,1}, // 往右走,此時t = 0
{1,0}, // 往下走,此時t = 1
{0,-1}, // 往左走,此時t = 2
{-1,0} }; // 往上走,此時t = 3
int left = 0, up = 0, right = n-1, bottom = n-1;
int num = 1 , t = 0 , row=0 , col=0;
while(num <= n*n){
arr[row][col] = num;
// col一直往右走,直到碰到右邊界(right)。若要超過右邊界了,則向量變為往下走
if(col + dirc[t][1] > right){
t = 1;
up++;
}
// row一直往下走,直到碰到下邊界(bottom)。若要超過下邊界了,則向量變為往左走
else if(row + dirc[t][0] > bottom){
t = 2;
right--;
}
// col一直往左走,直到碰到左邊界(left)。若要超過左邊界了,則向量變為往上走
else if(col + dirc[t][1] < left){
t = 3;
bottom--;
}
// row一直往上走,直到碰到上邊界(up)。若要超過上邊界了,則向量變為往上走
else if(row + dirc[t][0] < up){
t=0;
left++;
}
row += dirc[t][0];
col += dirc[t][1];
num++;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n ; j++){
if(j > 0) cout << ' ';
cout << arr[i][j];
}
cout << endl;
}
return 0;
}
hw4_tutorial
By Yei Yang
hw4_tutorial
made by yehyuyang
- 121