#include <bits/stdc++.h>
using namespace std;
int n, m;
int ttime;
int arr[1000][1000];
void s1(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s1(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s1(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s1(n,m-1, time);
}
}
}
void s2(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s2(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s2(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s2(n,m-1, time);
}
if (n>1&&arr[n-1][m] == 1) {
s2(n-1,m, time);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
int casee=1;
int s;
while (cin >> s) {
ttime = 1;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
if (s == 2) {
for(int i=0;i<m;i++){
s1(0,i, ttime);
}
} else {
for(int i=0;i<m;i++){
s2(0,i, ttime);
if(arr[0][i]!=0){
arr[0][i]=1;
}
}
}
cout<<"Case "<<casee<<":\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j]==1&&i!=0){
arr[i][j]=0;
}
cout << arr[i][j]<<" ";
}
cout << endl;
}
casee++;
}
}
#include
using namespace std;
int n, m;
int ttime;
int arr[1000][1000];
void s1(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s1(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s1(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s1(n,m-1, time);
}
}
}
void s2(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s2(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s2(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s2(n,m-1, time);
}
if (n>1&&arr[n-1][m] == 1) {
s2(n-1,m, time);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
int casee=1;
int s;
while (cin >> s) {
ttime = 1;cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
if (s == 2) {
for(int i=0;i
s1(0,i, ttime);
}
} else {
for(int i=0;i
s2(0,i, ttime);
if(arr[0][i]!=0){
arr[0][i]=1;
}
}
}
cout<<"Case "< for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j]==1&&i!=0){
arr[i][j]=0;
}
cout << arr[i][j]<<" ";
}
cout << endl;
}
casee++;
}
}
水管可能會繞一個圈
#include
using namespace std;
int n, m;
int ttime;
int arr[1000][1000];
void s1(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s1(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s1(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s1(n,m-1, time);
}
}
}
void s2(int n,int m, int time) {
if (arr[n][m] == 1) {
arr[n][m] = time;
time += 1;
if (arr[n + 1][m] == 1) {
s2(n + 1,m, time);
}
if (arr[n][m+1] == 1) {
s2(n,m+1, time);
}
if (arr[n][m-1] == 1) {
s2(n,m-1, time);
}
if (n>1&&arr[n-1][m] == 1) {
s2(n-1,m, time);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
int casee=1;
int s;
while (cin >> s) {
ttime = 1;cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
if (s == 2) {
for(int i=0;i
s1(0,i, ttime);
}
} else {
for(int i=0;i
s2(0,i, ttime);
if(arr[0][i]!=0){
arr[0][i]=1;
}
}
}
cout<<"Case "< for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j]==1&&i!=0){
arr[i][j]=0;
}
cout << arr[i][j]<<" ";
}
cout << endl;
}
casee++;
}
}
水管可能會繞一個圈
發現錯誤了,謝謝大神