20170908_hdu1495

debug:
中间过程出错,可以改写为函数
终止条件写错

#include 
#include 
#include 
#include 
#include 
using namespace std;

int mark[105][105][105];
int s,m,n;
int flag;

void dfs(int a, int b, int c, int step) {
    mark[a][b][c] = 1;
    if(b == s/2 || c == s/2) {
        cout << step << endl;
        flag = 1;
        return;
    }
    //a->b
    if(a <= (m-b)) {
        if(mark[0][b+a][c] == 0)
            dfs(0, b+a, c, step+1);
    }
    else {
        int t = a-(m-b);
        if(mark[t][b+a-t][c] == 0)
            dfs(t, b+a-t, c, step+1);
    }
    //a->c
    if(a <= (n-c)) {
        if(mark[0][b][c+a] == 0)
            dfs(0, b, c+a, step+1);
    }
    else {
        int t = a-(n-c);
        if(mark[t][b][c+a-t] == 0)
            dfs(t, b, c+a-t, step+1);
    }
    //b->a
    if(mark[a+b][0][c] == 0)
        dfs(a+b, 0, c, step+1);
    //b->c
    if(b <= (n-c)) {
        if(mark[a][0][b+c] == 0)
            dfs(a, 0, b+c, step+1);
    }
    else {
        int t = b - (n-c);
        if(mark[a][t][b+c-t] == 0)
            dfs(a, t, b+c-t, step+1);
    }
    //c->a
    if(mark[a+c][b][0] == 0)
        dfs(a+c, b, 0, step+1);
    //c->b
    if(c <= (m-b)) {
        if(mark[a][b+c][0])
            dfs(a, b+c, 0, step+1);
    }
    else {
        int t = c-(m-b);
        if(mark[a][b+c-t][t] == 0)
            dfs(a, b+c-t, t, step+1);
    }
    return;
}

int main() {
    while(scanf("%d %d %d",&s,&m,&n) && s && m && n) {
        memset(mark, 0, sizeof(mark));
        flag = 0;
        //cout << s << "   " << m << "   " << n << endl;
        if(s % 2 == 1)
            flag = 1;
        else
            dfs(s, 0, 0, 0);
        if(flag)
            cout << "NO" << endl;
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 
using namespace std;

int mark[105][105][105];
int s,m,n;
int flag;
struct state{
    int a;
    int b;
    int c;
    int step;
};


void bfs(state st) {
    state que[100005];
    memset(que, 0, sizeof(que));
    st.step = 0;
    int head = 0;
    int tail = 1;
    que[head] = st;
    mark[st.a][st.b][st.c] = 1;
    state now,next;
    while(head != tail) {
        now = que[head];
        if(now.b == s/2 || now.c == s/2) {
            cout << now.step << endl;
            if(now.a != 0)
                now.step++;
            flag = 0;
            return;
        }
        int aa,bb,cc;
        //a->b
        int t;
        if(now.a <= (m-now.b))
            t = 0;
        else t = now.a - (m-now.b);
        aa = t; bb = now.a+now.b-t; cc = now.c;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        //a->c
        if(now.a <= (n-now.c))
            t = 0;
        else t = now.a - n + now.c;
        aa = t; bb = now.b; cc = now.a+now.c-t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        //b->a
        aa = now.b + now.a; bb = 0; cc = now.c;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        //b->c
        if(now.b <= (n-now.c))
            t = 0;
        else t = now.b-n+now.c;
        aa = now.a; bb = t; cc = now.b + now.c - t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        //c->a
        aa = now.a + now.c; bb = now.b; cc = 0;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        //c->b
        if(now.c <= (m - now.b))
            t = 0;
        else t = now.c - m + now.b;
        aa = now.a; bb = now.b + now.c - t; cc = t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que[tail++] = next;
            mark[aa][bb][cc] = 1;
        }
        head++;
    }
}

int main() {
    while(scanf("%d %d %d",&s,&m,&n) && s && m && n) {
        memset(mark, 0, sizeof(mark));
        flag = 1;
        //cout << s << "   " << m << "   " << n << endl;
        state now;
        now.a = s;
        now.b = now.c = 0;
        if(s % 2 == 1)
            flag = 1;
        else
            bfs(now);
        if(flag)
            cout << "NO" << endl;
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int mark[105][105][105];
int s,m,n;
int flag;
struct state{
    int a;
    int b;
    int c;
    int step;
};


void bfs(state st) {
    queue que;
    st.step = 0;
    que.push(st);
    mark[st.a][st.b][st.c] = 1;
    state now,next;
    while(!que.empty()) {
        now = que.front();
        if((now.b == s/2 && now.a == s/2 )|| (now.a == s/2 && now.c == s/2) || (now.b == s/2 && now.c == s/2)) {
            cout << now.step << endl;
            flag = 0;
            return;
        }
        int aa,bb,cc;
        //a->b
        int t;
        if(now.a <= (m-now.b))
            t = 0;
        else t = now.a - (m-now.b);
        aa = t; bb = now.a+now.b-t; cc = now.c;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        //a->c
        if(now.a <= (n-now.c))
            t = 0;
        else t = now.a - n + now.c;
        aa = t; bb = now.b; cc = now.a+now.c-t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        //b->a
        aa = now.b + now.a; bb = 0; cc = now.c;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        //b->c
        if(now.b <= (n-now.c))
            t = 0;
        else t = now.b-n+now.c;
        aa = now.a; bb = t; cc = now.b + now.c - t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        //c->a
        aa = now.a + now.c; bb = now.b; cc = 0;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        //c->b
        if(now.c <= (m - now.b))
            t = 0;
        else t = now.c - m + now.b;
        aa = now.a; bb = now.b + now.c - t; cc = t;
        if(mark[aa][bb][cc] != 1) {
            next.a = aa; next.b = bb; next.c = cc;
            next.step = now.step+1;
            que.push(next);
            mark[aa][bb][cc] = 1;
        }
        que.pop();
    }
}

int main() {
    while(scanf("%d %d %d",&s,&m,&n) && s && m && n) {
        memset(mark, 0, sizeof(mark));
        flag = 1;
        //cout << s << "   " << m << "   " << n << endl;
        state now;
        now.a = s;
        now.b = now.c = 0;
        if(s % 2 == 1)
            flag = 1;
        else
            bfs(now);
        if(flag)
            cout << "NO" << endl;
    }
    return 0;
}
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int a[17][17];
int b[17][17];
int c[17][17];
int ans[17][17];

void change(int i, int j) {
    if(b[i][j] == 1)
        b[i][j] = 0;
    else b[i][j] = 1;
}

void manage(int i, int j) {
    change(i,j);
    change(i-1,j);
    change(i,j-1);
    change(i+1,j);
    change(i,j+1);
    c[i][j] = 1;
}

int main() {
    int m,n;
    cin >> m;
    cin >> n;
    int max = 1;
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= n; ++j)
            scanf("%d", &a[i][j]);
    for(int i = 0; i < n; ++i)
        max *= 2;
    int t = 300;
    for(int i = 0; i < max; ++i) {
        memset(c, 0, sizeof(c));
        int count = 0;
        for(int i = 1; i <= m; ++i)
            for(int j = 1; j <= n; ++j)
                b[i][j] = a[i][j];
        int cnt = 1;
        int tt = i;
        while(tt >= 1) {
            if((tt & 1) == 1) {
                manage(1,cnt);
                count++;
            }
            tt = tt >> 1;
        }
        int flag = 0;
        for(int i = 2; i <= m; ++i)
            for(int j = 1; j <= n; ++j) {
                if(b[i-1][j] == 1) {
                    manage(i,j);
                    count++;
                }
            }
        for(int j = 1; j <= n; ++j)
            if(b[m][j] == 1)
                flag = 1;
        if(t > count && flag == 0) {
            t = count;
            for(int i = 1; i <= m; ++i)
                for(int j = 1; j <= n; ++j)
                    ans[i][j] = c[i][j];
        }
    }
    if(t == 300)
        cout << "IMPOSSIBLE" << endl;
    else for(int i = 1; i <= m; ++i) {
        for(int j = 1; j <= n; ++j) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(20170908_hdu1495)