いろいろがんばりたいブログ

情報科学科の人がいろいろ書きます。

AOJ 2311 Dessert Witch

やるだけでした。
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2311

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;


const int dx[] = {1,1,1,0,0,-1,-1,-1};
const int dy[] = {1,0,-1,1,-1,1,0,-1};
const double EPS = 1e-9;

void cerrTable(vector<vector<char> > table){
    for(int i=0;i<table.size();i++){
        for(int j=0;j<table[i].size();j++){
            cerr << table[i][j] << " ";
        }
        cerr << endl;
    }
}

int main(){
    int N = 8;
    const char mami='o',charlotte='x',empty='.',wall='z';
    vector<vector<char> > table(N+2,vector<char>(N+2,wall));
    for(int y=1;y<=N;y++){
        for(int x=1;x<=N;x++){
            cin >> table[y][x];
        }
    }

    for(bool updated=true;updated;){
        updated = false;

        int mamiy,mamix,maxmami=0;
        // mami
        for(int y=1;y<=N;y++){
            for(int x=1;x<=N;x++){
                if(table[y][x] == empty){
                    int cnt = 0;
                    for(int i=0;i<8;i++){
                        int ny = y+dy[i],nx=x+dx[i];
                        if(table[ny][nx] == charlotte){
                            int localcnt = 0;
                            while(table[ny][nx] == charlotte){
                                localcnt++;
                                ny += dy[i],nx+=dx[i];
                            }
                            if(table[ny][nx] == mami){
                                cnt += localcnt;
                            }
                        }
                    }
                    if(cnt > maxmami){
                        maxmami = cnt;
                        mamiy = y;
                        mamix = x;
                    }
                }
            }
        }

        if(maxmami != 0){
            table[mamiy][mamix] = mami;
            for(int i=0;i<8;i++){
                int ny = mamiy + dy[i];
                int nx = mamix + dx[i];
                if(table[ny][nx] == charlotte){
                    while(table[ny][nx] == charlotte){
                        ny += dy[i],nx+=dx[i];
                    }
                    if(table[ny][nx] == mami){
                        ny -= dy[i],nx-=dx[i];
                        while(ny != mamiy or nx != mamix){
                            table[ny][nx] = mami;
                            ny -= dy[i],nx -= dx[i];
                        }
                    }
                }
            }
        }

        int charlottey,charlottex,maxcharlotte=0;
        // charlotte
        for(int y=1;y<=N;y++){
            for(int x=1;x<=N;x++){
                if(table[y][x] == empty){
                    int cnt = 0;
                    for(int i=0;i<8;i++){
                        int ny = y+dy[i],nx=x+dx[i];
                        if(table[ny][nx] == mami){
                            int localcnt = 0;
                            while(table[ny][nx] == mami){
                                localcnt++;
                                ny += dy[i],nx+=dx[i];
                            }
                            if(table[ny][nx] == charlotte){
                                cnt += localcnt;
                            }
                        }
                    }
                    if(cnt >= maxcharlotte){
                        maxcharlotte = cnt;
                        charlottey = y;
                        charlottex = x;
                    }
                }
            }
        }

        if(maxcharlotte != 0){
            table[charlottey][charlottex] = charlotte;
            for(int i=0;i<8;i++){
                int ny = charlottey + dy[i];
                int nx = charlottex + dx[i];
                if(table[ny][nx] == mami){
                    while(table[ny][nx] == mami){
                        ny += dy[i],nx+=dx[i];
                    }
                    if(table[ny][nx] == charlotte){
                        ny -= dy[i],nx-=dx[i];
                        while(ny != charlottey or nx != charlottex){
                            table[ny][nx] = charlotte;
                            ny -= dy[i],nx -= dx[i];
                        }
                    }
                }
            }
        }

        if(maxmami != 0 or maxcharlotte != 0){
            updated = true;
        }
    }

    for(int i=1;i<=8;i++){
        for(int j=1;j<=8;j++){
            cout << table[i][j];
        }
        cout << endl;
    }
    return 0;
}