##質問内容
競技プログラミングの閉路検出の問題(yukicoder:No.13 )を解きました.
返り値のとり方を変えることで答えが違う結果になりました.
(違う結果というのは全ての結果を試した上で,impossible,possibleの出力が異なる結果が出てしまったということです)
間違えたテスト結果を元に色々な方法で確認したのですがあまり理解できませんでした.
どうして異なる結果(例:テストケース1)になるのかを教えていただきたいです.
正解では,グローバル変数のflagを変更し,結果を出力し,
不正解では,関数の返り値の値によって結果を判定しています.
おそらく,グローバル変数のflagを変更しないとbool dfs()の関数をreturnしても,rep(i,4)のループの中で最終的にfalseが返って来た時に異なる結果が出力されるのではないかと考えています.
また、このようなミスに気づくためにはこれからどう対応すれば良いのでしょうか?
ご回答よろしくお願いします.
##コード
正解になったコード
c
1#define ll long long 2#define ffor(i,a,b) for (int i=(a);i<(b);i++) 3#define rfor(i,a,b) for (int i=(b)-1;i>=(a);i--) 4#define rep(i,n) for (int i=0;i<(n);i++) 5#define rrep(i,n) for (int i=(n)-1;i>=0;i--) 6#include<iostream> 7#include<iomanip> 8#include<algorithm> 9#include<cmath> 10#include<string> 11#include<stack> 12#include<queue> 13#include<vector> 14#include<map> 15#define SIZE 100001 16#define MOD 1000000007 17#define INF 100000000 18using namespace std; 19 20int w,h; 21int field[101][101]; 22int used[101][101]; 23bool d[101][101]; 24int dx[4] = {1, 0, -1, 0}; 25int dy[4] = {0, 1, 0, -1}; 26bool flag = false; 27 28void bfs(int y, int x, int py, int px, int num){ 29 d[y][x] = true; used[y][x] = 1; 30 rep(i, 4){ 31 int nowy = y + dy[i], nowx = x + dx[i]; 32 if(nowy < 0 || h <= nowy || nowx < 0 || w <= nowx) continue; 33 //戻るの禁止 34 if(nowy == py && nowx == px) continue; 35 //違う番号なら 36 if(field[nowy][nowx] != num) continue; 37 //もし一度訪れた場所に戻るならflagをtrueにして終了 38 if(used[nowy][nowx]){ 39 flag = true; 40 return; 41 } 42 bfs(nowy, nowx, y, x, num); 43 } 44 45 return; 46} 47 48int main(){ 49 cin.tie(0); 50 ios::sync_with_stdio(false); 51 52 cin >> w >> h; 53 rep(i,h)rep(j,w) cin >> field[i][j]; 54 55 rep(i,h)rep(j,w){ 56 if(d[i][j]) continue; 57 rep(a,101)rep(b,101) used[a][b] = 0; 58 bfs(i, j, -1, -1, field[i][j]); 59 } 60 61 cout << ((flag)? "possible":"impossible") << endl; 62 return 0; 63}
不正解
c
1#define ll long long 2#define ffor(i,a,b) for (int i=(a);i<(b);i++) 3#define rfor(i,a,b) for (int i=(b)-1;i>=(a);i--) 4#define rep(i,n) for (int i=0;i<(n);i++) 5#define rrep(i,n) for (int i=(n)-1;i>=0;i--) 6#include<iostream> 7#include<iomanip> 8#include<algorithm> 9#include<cmath> 10#include<string> 11#include<stack> 12#include<queue> 13#include<vector> 14#include<map> 15#define SIZE 100001 16#define MOD 1000000007 17#define INF 100000000 18using namespace std; 19 20int w,h; 21int field[101][101]; 22int used[101][101]; 23bool d[101][101]; 24int dx[4] = {1, 0, -1, 0}; 25int dy[4] = {0, 1, 0, -1}; 26bool flag = false; 27 28bool bfs(int y, int x, int py, int px, int num){ 29 d[y][x] = true; used[y][x] = 1; 30 rep(i, 4){ 31 int nowy = y + dy[i], nowx = x + dx[i]; 32 if(nowy < 0 || h <= nowy || nowx < 0 || w <= nowx) continue; 33 //戻るの禁止 34 if(nowy == py && nowx == px) continue; 35 //違う番号なら 36 if(field[nowy][nowx] != num) continue; 37 //もし一度訪れた場所に戻るならflagをtrueにして終了 38 if(used[nowy][nowx]) return true; 39 bfs(nowy, nowx, y, x, num); 40 41 } 42 43 return false; 44} 45 46int main(){ 47 cin.tie(0); 48 ios::sync_with_stdio(false); 49 50 cin >> w >> h; 51 rep(i,h)rep(j,w) cin >> field[i][j]; 52 53 rep(i,h)rep(j,w){ 54 if(d[i][j]) continue; 55 rep(a,101)rep(b,101) used[a][b] = 0; 56 if(bfs(i, j, -1, -1, field[i][j])){ 57 cout << "possible" << endl; 58 return 0; 59 } 60 } 61 cout << "impossible" << endl; 62 //cout << ((flag)? "possible":"impossible") << endl; 63 return 0; 64}
回答3件
あなたの回答
tips
プレビュー