#4974: while(1)會TLE


CSE911439 (資料結構演算法)


/**********************************************************************************/
/*  Problem: a016 "數獨(SUDOKU)"                                                */
/*  Language: CPP                                                                 */
/*  Result: WA (line:2) on ZeroJudge                                              */
/*  Author: CSE911439 at 2011-03-13 13:47:08                                      */
/**********************************************************************************/


#include <iostream>
using namespace std;
int sqcheck (int i, int j) {
    if (i>=0 && i<=2) {
        if (j>=0 && j<=2) {
            return 0;
        }
        else if (j>=3 && j<=5) {
            return 1;
        }
        else if (j>=6 && j<=8) {
            return 2;
        }
    }
    else if (i>=3 && i<=5) {
        if (j>=0 && j<=2) {
            return 3;
        }
        else if (j>=3 && j<=5) {
            return 4;
        }
        else if (j>=6 && j<=8) {
            return 5;
        }
    }
    else if (i>=6 && i<=8) {
        if (j>=0 && j<=2) {
            return 6;
        }
        else if (j>=3 && j<=5) {
            return 7;
        }
        else if (j>=6 && j<=8) {
            return 8;
        }
    }
}
int main () {
    while (cin) {
        int a[9][9], i, j, k;
        bool b[3][9][9]={false}, f=false;
        for (i=0;i<9;i++) {
            for (j=0;j<9;j++) {
                cin >> a[i][j];
            }
        }
        for (i=0;i<9;i++) {
            for (j=0;j<9;j++) {
                if (b[0][i][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[0][i][a[i][j]]=true;
                if (b[1][j][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[1][j][a[i][j]]=true;
                k=sqcheck(i,j);
                if (b[2][k][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[2][k][a[i][j]]=true;
            }
            if (f==true) {
                break;
            }
        }
        if(f==false) cout << "yes"<< endl;
        else cout << "no"<< endl;
    }
    return 0;
}
 
 
幫忙看一下
不是TLE就是WA
... 
#5075: Re:while(1)會TLE


abcd6891 (曄哥)


/**********************************************************************************/
/*  Problem: a016 "數獨(SUDOKU)"                                                */
/*  Language: CPP                                                                 */
/*  Result: WA (line:2) on ZeroJudge                                              */
/*  Author: CSE911439 at 2011-03-13 13:47:08                                      */
/**********************************************************************************/


#include
using namespace std;
int sqcheck (int i, int j) {
    if (i>=0 && i<=2) {
        if (j>=0 && j<=2) {
            return 0;
        }
        else if (j>=3 && j<=5) {
            return 1;
        }
        else if (j>=6 && j<=8) {
            return 2;
        }
    }
    else if (i>=3 && i<=5) {
        if (j>=0 && j<=2) {
            return 3;
        }
        else if (j>=3 && j<=5) {
            return 4;
        }
        else if (j>=6 && j<=8) {
            return 5;
        }
    }
    else if (i>=6 && i<=8) {
        if (j>=0 && j<=2) {
            return 6;
        }
        else if (j>=3 && j<=5) {
            return 7;
        }
        else if (j>=6 && j<=8) {
            return 8;
        }
    }
}
int main () {
    while (cin) {
        int a[9][9], i, j, k;
        bool b[3][9][9]={false}, f=false;
        for (i=0;i<9;i++) {
            for (j=0;j<9;j++) {
                cin >> a[i][j];
            }
        }
        for (i=0;i<9;i++) {
            for (j=0;j<9;j++) {
                if (b[0][i][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[0][i][a[i][j]]=true;
                if (b[1][j][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[1][j][a[i][j]]=true;
                k=sqcheck(i,j);
                if (b[2][k][a[i][j]]==true) {
                    f=true;
                    break;
                }
                else b[2][k][a[i][j]]=true;
            }
            if (f==true) {
                break;
            }
        }
        if(f==false) cout << "yes"<< endl;
        else cout << "no"<< endl;
    }
    return 0;
}
 
 
幫忙看一下
不是TLE就是WA
... 

試著從數學性質下手

不要完全使用if else