Demo entry 6761622

不能更弱了

   

Submitted by anonymous on Sep 30, 2018 at 05:11
Language: cpp-objdump. Code size: 12.1 kB.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include<ctime>
#include <utility>  // pair
#include <sstream>
#include <algorithm>
#include <cmath>

typedef long long ll;

using namespace std;

// 棋盘大小
const int Row = 15;
const int Col = 15;

const ll Maxx = 1<<30;
/*       棋形分数
    活四 ?AAAA? 300000
    死四A AAAA? 2500
    死四B AAA?A 3000
    死四C AA?AA 2600
    活三 ??AAA?? 3000
    死三A AAA?? 500
    死三B ?A?AA? 800
    死三C A??AA 600
    死三D A?A?A 550
    活二 ???AA??? 650
    死二A AA??? 150
    死二B ??A?A?? 250
    死二C ?A??A? 200

void renju_score(){

    int dir[4][2] = {{0,1},{1,0},{1,1},{-1,1} ;
    for(int i=0; i<Row-5 ; ++i){

        for(int j=0;j<Col-5; ++j){
                string s;
                s.clear();
                for(int k=0;k<4;++k){
                    int x = dir[k][0];
                    int y = dir[k][1];

                }

        }

    }

}
*/

const int Four = 300000;
const int SFour1 = 2500 ;
const int SFour2 = 3000 ;
const int SFour3 = 2600 ;
const int Three  = 3000 ;
const int SThree1 = 500 ;
const int SThree2 = 800 ;
const int SThree3 = 600 ;
const int SThree4 = 550 ;
const int Two = 650 ;
const int STwo = 150 ;
const int STwo2 = 250 ;
const int STwo3 = 200 ;

const int maxx = 1<<30;

int score_board[16][16] = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
    {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0},
    {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0},
    {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0},
    {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0},
    {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0},
    {0,1,2,3,4,5,6,7,6,5,4,3,2,1,0},
    {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0},
    {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0},
    {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0},
    {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0},
    {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};

// 棋盘
int Board[Row][Col] ;

// init
void Init(){
    for(int i = 0 ;i < Row ; ++i){
        for(int j = 0;j < Col ; ++j){
            Board[i][j] = 0 ;
        }
    }
}

inline int max(int x , int y){
    return x > y ? x : y ;
}
inline int min(int x , int y){
    return x > y ? y : x ;
}

// 复原棋盘
void placeAt(const int& x ,const int& y ,const int& type){
    if(x >=0 && y >=0 ){
        Board[x][y] = type;
    }
}

// possible position and value
struct psb_pos{
    int x , y ;
    int value ;
};


//judge who win the game or not yet
 int isEnd(int x, int y, int color) {
    int dx[] = {1, 0, 1, 1};
    int dy[] = {0, 1, 1, -1};

    for (int i = 0; i < 4; i++) {
        int cnt = 1;

        int tx = x + dx[i];
        int ty = y + dy[i];
        while (tx >= 0 && tx < Row && ty >= 0 && ty < Col && Board[tx][ty] == color) {
            tx += dx[i];
            ty += dy[i];
            ++cnt;
        }

        tx = x - dx[i];
        ty = y - dy[i];
        while (tx >= 0 && tx < Row && ty >= 0 && ty < Col && Board[tx][ty] == color) {
            tx -= dx[i];
            ty -= dy[i];
            ++cnt;
        }

        if(cnt >= 5)
            return color;
    }
    return false;
}

/*
估值有待重写
*/
int eval(int x , int y , int color , int ans){
    int dx[] = {1, 0, 1, 1};
    int dy[] = {0, 1, 1, -1};
    int num[2][100] = {0};

    for (int i = 0; i < 4; i++) {
        int sum = 1;
        int flag1 = 0, flag2 = 0;

        int tx = x + dx[i];
        int ty = y + dy[i];
        while (tx >= 0 && tx < Row
                && ty >= 0 && ty < Col
                && Board[tx][ty] == color) {
            tx += dx[i];
            ty += dy[i];
            ++sum;
        }

        if(tx >= 0 && tx < Row
                && ty >= 0 && ty < Col
                && Board[tx][ty] == 0)
            flag1 = 1;

        tx = x - dx[i];
        ty = y - dy[i];
        while (tx >= 0 && tx < Row
                && ty >= 0 && ty < Col
                && Board[tx][ty] == color) {
            tx -= dx[i];
            ty -= dy[i];
            ++sum;
        }

        if(tx >= 0 && tx < Row
                && ty >= 0 && ty < Col
                && Board[tx][ty] == 0)
            flag2 = 1;

        if(flag1 + flag2 > 0)
            ++num[flag1 + flag2 - 1][sum];
    }

//    cout<<"come"<<endl;
    // five
    if(num[0][5] + num[1][5] > 0)
        ans = max(ans, 1000000);
    // 活4 | 双死四 | 死4活3
    else if(num[1][4] > 0
            || num[0][4] > 1
            || (num[0][4] > 0 && num[1][3] > 0)) {
            ans = max(ans, 300000);
    }
    //双活3
    else if(num[1][3] > 1)
        ans = max(ans, 10000);
    //死3活3
    else if(num[1][3] > 0 && num[0][3] > 0)
        ans = max(ans, 5000);
    //死4
    else if(num[0][4] > 0)
    {
        ans = max(ans, 4000);
    }
    //单活3
    else if(num[1][3] > 0)
        ans = max(ans, 3500);
    //双活2
    else if(num[1][2] > 1)
        ans = max(ans, 1000);
    //死三
    else if(num[0][3] > 0)
    {
        ans = max(ans, 500);
    }
    //单活2
    else if(num[1][2] > 0)
        ans = max(ans, 100);
    // 死2
    else if(num[0][2] > 0)
        ans = max(ans, 10);
    else  ans = max(ans, 0);

    return ans;
}

// 落子区域
void fill_psb_pos(int x , int y ,  bool temp_board[][Col]){
    int row_bottow = 0;
    int row_top = Row ;
    int col_left = Col;
    int col_right = 0;

    if(x - 2 >= 0) row_top=x-2;
    else if( x -1 >=0) row_top = x-1;
    else row_top = x;

    if(x+2<Row)row_bottow = x+2;
    else if(x+1<Row) row_bottow = x+1;
    else row_bottow = x;

    if(y-2>=0) col_left = y-2;
    else if(y-1>=0) col_left = y-1;
    else col_left = y;

    if(y+2<Col) col_right = y+2;
    else if(y+1<Col) col_right = y+1;
    else col_right = y;

    for(int i = row_top ; i <= row_bottow ; ++i){
        for(int j = col_left ; j <= col_right ; ++j) {
            temp_board[i][j] = true;
        }
    }
}

bool cmp(psb_pos a , psb_pos b){

    return a.value > b.value;  // == 不能= 号, c++ 都忘了。。

}

struct ans_pos{

    int x ,  y;

}position;

//搜索深度

int Depth ;
//局面估值

int eval_board(int color){
    int Max1 = -1;
    int Max2 = -1;
    for(int i=0;i<Row ;++i){
        for(int j= 0;j<Col;++j){
            if(Board[i][j] == color%2+1){
                Max1 = eval(i,j,1,Max1) ;
            }
            else if(Board[i][j] == color){
                Max2 = eval(i,j,1,Max2);
            }
        }
    }
    return  Max1 - Max2;
}

int alhpa_beta_search(int alpha , int beta , int depth , int color , int prex, int prey)  {

        if(depth >= Depth || (prex != -1 && isEnd(prex, prey, color%2+1) != 0)   ) {

            int ans = eval_board(color) ;

            if(depth % 2 == 0)

                ans = -ans;

            return ans;
        }

        int num = 0;

        int ans = -100000000;

        psb_pos possible[300];

        bool temp_board[Row][Col] = {0};

        for(int x=0; x<Row; ++x){
            for(int y=0;y<Col;++y){
                if(Board[x][y]) {
                    fill_psb_pos(x,y,temp_board);
                }
            }
        }

        for(int x=0; x< Row; ++x){
        	for(int y=0; y< Col; ++y){
        		if(Board[x][y]||temp_board[x][y]==0)
        			continue;
        		Board[x][y] = color;
        		possible[num].x= x;
        		possible[num].y= y;
        		possible[num++].value = score_board[x][y] + eval(x, y, color,ans) + eval(x, y, color%2+1,ans);
        		Board[x][y] = 0;
        	}
        }

        sort(possible,possible+num,cmp);

        num = min(num,7);
//        cout<<"num"<<num<<endl;

         for(int i=0 ; i < num  ; ++i){
                int x = possible[i].x ;
                int y = possible[i].y ;
                Board[x][y] = color;

                int val = -alhpa_beta_search( -beta, -alpha,depth + 1 , color%2+1 , x , y);

                Board[x][y] = 0;

                if(val >= beta)

                    return beta;

                if(val > alpha)

                    alpha = val;
        }
//        cout<<"alpha"<<alpha<<endl;
        return alpha;

}
void Find(){
    int num = 0;

        int ans = -100000000;

        psb_pos possible[300];

        bool temp_board[Row][Col] = {0};

        for(int x=0; x<Row; ++x){
            for(int y=0;y<Col;++y){
                if(Board[x][y]) {
                    fill_psb_pos(x,y,temp_board);
                }
            }
        }

        for(int x=0; x< Row; ++x){
        	for(int y=0; y< Col; ++y){
        		if(Board[x][y]||temp_board[x][y]==0)
        			continue;
        		Board[x][y] = 1;
        		possible[num].x= x;
        		possible[num].y= y;
        		possible[num++].value = score_board[x][y] + eval(x, y,1,ans) + eval(x, y, 2 ,ans);
        		Board[x][y] = 0;
        	}
        }

        sort(possible,possible+num,cmp);

        num = min(num,7);
//        cout<<"num"<<num<<endl;

         for(int i=0 ; i < num  ; ++i){
                int x = possible[i].x ;
                int y = possible[i].y ;
                Board[x][y] = 1;

                int val = -alhpa_beta_search( -100000000, 100000000, 0 , 2 , x , y);

             if(val > ans) {

                    ans = val;
                    position.x = x ;
                    position.y = y ;

                }

                Board[x][y] = 0;

    }
    return ;
}
//#define debug 1
#ifdef debug

void Debug(){

    for(int i=0; i<Row; ++i){
        for(int j=0; j<Col; ++j){
            if(Board[i][j] == 1){
                cout<<"X";
            }
            else if(Board[i][j] == 2){
                cout<<"O";
            }
            else {
                cout<<".";
            }
        }
            cout<<endl;
    }
    cout<<endl;
    return;

}

void input(){
    int x , y;
    Depth = 2;
    while(true){
        cout<<"pls input " <<endl;
        cin>> x >> y;
        Board[x][y] = 2;
        Find();
//        alhpa_beta_search(-10000000 , 10000000 , 0 ,1,  -1 , -1);
        cout << position.x << " " << position.y << endl;
        Board[position.x][position.y] = 1;
        Debug();
        if( x==-1&&y==-1)break;
    }
}

#endif // debug

#define bot 1
#ifdef bot

#include "jsoncpp/json.h"  // C++编译时默认包含此库
using namespace Json;
// AI落子


Value Write(const int& x ,const int& y) {
   Json::Value decesion;
   decesion["x"] = x;
   decesion["y"] = y;
   return decesion;
}


bool start() {
	for(int i=0;i<15;i++)
		for(int j=0;j<15;j++)
			if(Board[i][j]!=0) return 0;
	return 1;
}

#endif // bot


int main() {
   // 初始化棋盘
    Init();
    Depth = 6;
//    input();
   	string str;
	getline(cin, str);
	Reader reader;
	Value input;
	reader.parse(str, input);
	int turnID = input["responses"].size();
	for (int i = 0; i < turnID; i++) {
		placeAt(input["requests"][i]["x"].asInt(),input["requests"][i]["y"].asInt(),2);
		placeAt(input["responses"][i]["x"].asInt(),input["responses"][i]["y"].asInt(),1);
	}
	placeAt(input["requests"][turnID]["x"].asInt(),input["requests"][turnID]["y"].asInt(),2);
	Value ret;

	int id = 2;
	if(turnID>=2 && input["requests"][id]["x"].asInt()== -1 && input["requests"][id]["y"].asInt()==-1){
        int rq = 1, rp = 0 , rp2 = 1;
        placeAt(input["requests"][rq]["x"].asInt(), input["requests"][rq]["y"].asInt(),1);
		placeAt(input["responses"][rp]["x"].asInt(), input["responses"][rp]["y"].asInt(),2);
        placeAt(input["responses"][rp2]["x"].asInt(), input["responses"][rp2]["y"].asInt(),2);
	}

	if(turnID==0&&input["requests"][id]["x"].asInt()== -1 && input["requests"][id]["y"].asInt()==-1) {
		ret["response"]=Write(0,0);
	}
	 else {
        Find();
		ret["response"]=Write(position.x,position.y);
	}
	FastWriter writer;
	cout<<writer.write(ret)<<endl;

    return 0;
}

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).