#include <iostream>
#include <time.h>
#include "map"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "array"
using namespace std; #define NULL (0) #define OUT_X (0b1111)
#define OUT_POS (0b11111111)
#define OUT_BLK (0b11111111111111111111111111111111) #define get_x_from_pos(pos) (((pos)>>4)&0b1111)
#define get_y_from_pos(pos) ((pos)&0b1111) #define get_pos_from_xy(x,y) (((x)<<4)|(y)) #define get_u_pos_from_blk(stBlk) (((stBlk)>>24)&0b11111111)
#define get_d_pos_from_blk(stBlk) (((stBlk)>>16)&0b11111111)
#define get_l_pos_from_blk(stBlk) (((stBlk)>>8)&0b11111111)
#define get_r_pos_from_blk(stBlk) ((stBlk)&0b11111111) #define get_blk_from_pos_u_d_l_r(u,d,l,r) (((u)<<24)|((d)<<16)|((l)<<8)|(r)) #define set_u_pos_in_blk(stBlk,pos) ((stBlk)=(((stBlk)&0b00000000111111111111111111111111)|(pos<<24)))
#define set_d_pos_in_blk(stBlk,pos) ((stBlk)=(((stBlk)&0b11111111000000001111111111111111)|(pos<<16)))
#define set_l_pos_in_blk(stBlk,pos) ((stBlk)=(((stBlk)&0b11111111111111110000000011111111)|(pos<<8)))
#define set_r_pos_in_blk(stBlk,pos) ((stBlk)=(((stBlk)&0b11111111111111111111111100000000)|(pos))) typedef unsigned char pos;
typedef unsigned int stBlk;
typedef unsigned int color; // global
array<array<color, 10>, 10> g_initColorMap;
array<array<stBlk, 10>, 10> g_initStateMap;
pos g_initHeadPos;
map<array<array<stBlk, 10>, 10>, int> g_SCORE;
int g_BEST_SCORE;
int g_mapSize, g_mapSize_old; void globalInit()
{
for (int i=1;i<=10;++i) g_initColorMap[i].fill(0);
for (int i=1;i<=10;++i) g_initStateMap[i].fill(0);
g_initHeadPos = 0;
g_SCORE.clear();
g_BEST_SCORE = 1;
g_mapSize = 0;
g_mapSize_old = 0;
} void readInitMap()
{
/*
2441353511
2122511244
3444543422
2131423443
4241445212
1122411534
1251131145
2441444415
4254453131
3321521455
*/
string x = "2441353511212251124434445434222131423443424144521211224115341251131145244144441542544531313321521455";
// read color map
for (int i=0; i<=9; ++i) {
for (int j=0; j<=9; ++j) {
g_initColorMap[i][j] = x[i+10*(9-j)]-'0';
}
}
// init state map
for (int i=0; i<=9; ++i) {
for (int j=0; j<=9; ++j) {
pos up, down, left, right;
if (j==9) up = OUT_POS; else up = get_pos_from_xy(i, j+1);
if (j==0) down = OUT_POS; else down = get_pos_from_xy(i, j-1);
if (i==0) left = OUT_POS; else left = get_pos_from_xy(i-1, j);
if (i==9) right = OUT_POS; else right = get_pos_from_xy(i+1, j);
//printf("g_initStateMap[%d][%d]=(%d,%d),(%d,%d),(%d,%d),(%d,%d)\n", i, j, \
get_x_from_pos(up), get_y_from_pos(up), \
get_x_from_pos(down), get_y_from_pos(down), \
get_x_from_pos(left), get_y_from_pos(left), \
get_x_from_pos(right), get_y_from_pos(right));
g_initStateMap[i][j] = get_blk_from_pos_u_d_l_r(up, down, left, right);
}
}
g_initHeadPos = get_pos_from_xy(0, 0);
} void printMap(array<array<stBlk, 10>, 10> stMp, pos RealHeadPos)
{
if (stMp[get_x_from_pos(RealHeadPos)][get_y_from_pos(RealHeadPos)] == 0) {
printf("printMap ERROR, RealHeadPos is NULL (%d,%d)\n", get_x_from_pos(RealHeadPos), get_y_from_pos(RealHeadPos));
return;
}
array<array<color, 10>, 10> temp;
for (int i=0; i<10; ++i) temp[i].fill(0);
pos temp2[10][10];
for(int i=0; i<10; ++i) for(int j=0; j<10; ++j) {
temp2[i][j] = OUT_POS;
} int xx = 0;
pos p = RealHeadPos;
while (p!=OUT_POS) {
xx++;
pos q = p;
int yy = 0;
while (q!=OUT_POS) {
yy++;
char qx = get_x_from_pos(q);
char qy = get_y_from_pos(q);
temp[xx-1][yy-1] = g_initColorMap[qx][qy];
temp2[xx-1][yy-1] = q;
q = get_u_pos_from_blk(stMp[qx][qy]);
}
p = get_r_pos_from_blk(stMp[get_x_from_pos(p)][get_y_from_pos(p)]);
} cout << "Head Pos: (" << get_x_from_pos(RealHeadPos) << "," << get_y_from_pos(RealHeadPos) << ")" << endl;
for (int j=9; j>=0; --j) {
for (int i=0; i<=9; ++i) {
//if (temp[i][j] == 0) cout<<" "; else cout<<temp[i][j];
if (temp2[i][j] == OUT_POS) printf(" "); else printf("(%d,%d) ",get_x_from_pos(temp2[i][j]),get_y_from_pos(temp2[i][j]));
}
cout<<endl;
}
cout<<endl;
} void tickOneBlk(array<array<stBlk, 10>, 10> &S, pos &Hp, pos tickP)
{
char tickPx = get_x_from_pos(tickP);
char tickPy = get_y_from_pos(tickP); stBlk tickBlk = S[tickPx][tickPy];
if (tickBlk == 0) {
for(int i=0;i<1000;++i) cout <<"fuck"<<endl;
return;
} pos up = get_u_pos_from_blk(tickBlk);
char upx = get_x_from_pos(up);
char upy = get_y_from_pos(up);
pos dp = get_d_pos_from_blk(tickBlk);
char dpx = get_x_from_pos(dp);
char dpy = get_y_from_pos(dp); // at least 1 block below it.
if (dp != OUT_POS) {
if (up == OUT_POS) {
set_u_pos_in_blk(S[dpx][dpy], OUT_POS);
}
else {
set_u_pos_in_blk(S[dpx][dpy], up);
set_d_pos_in_blk(S[upx][upy], dp);
}
}
else if (dp == OUT_POS) { // it's the bottom block
pos lp = get_l_pos_from_blk(tickBlk);
char lpx = get_x_from_pos(lp);
char lpy = get_y_from_pos(lp);
pos rp = get_r_pos_from_blk(tickBlk);
char rpx = get_x_from_pos(rp);
char rpy = get_y_from_pos(rp);
if (up == OUT_POS) {
if (lp != OUT_POS) {
if (rp == OUT_POS) {
set_r_pos_in_blk(S[lpx][lpy], OUT_POS);
}
else if (rp != OUT_POS) {
set_l_pos_in_blk(S[rpx][rpy], lp);
set_r_pos_in_blk(S[lpx][lpy], rp);
}
}
else if (lp == OUT_POS) {
if (rp == OUT_POS) {
Hp = OUT_POS;
}
else if (rp != OUT_POS) {
set_l_pos_in_blk(S[rpx][rpy], OUT_POS);
Hp = rp;
}
}
}
else if (up != OUT_POS){
if (lp != OUT_POS) {
if (rp == OUT_POS) {
set_r_pos_in_blk(S[lpx][lpy], up);
set_d_pos_in_blk(S[upx][upy], dp);
set_l_pos_in_blk(S[upx][upy], lp);
set_r_pos_in_blk(S[upx][upy], rp);
}
else if (rp != OUT_POS) {
set_r_pos_in_blk(S[lpx][lpy], up);
set_d_pos_in_blk(S[upx][upy], dp);
set_l_pos_in_blk(S[upx][upy], lp);
set_r_pos_in_blk(S[upx][upy], rp);
set_l_pos_in_blk(S[rpx][rpy], up);
}
}
else if (lp == OUT_POS) {
if (rp == OUT_POS) {
set_d_pos_in_blk(S[upx][upy], dp);
set_l_pos_in_blk(S[upx][upy], lp);
set_r_pos_in_blk(S[upx][upy], rp);
Hp = up;
}
else if (rp != OUT_POS) {
set_d_pos_in_blk(S[upx][upy], dp);
set_r_pos_in_blk(S[upx][upy], rp);
set_l_pos_in_blk(S[upx][upy], lp);
set_l_pos_in_blk(S[rpx][rpy], up);
Hp = up;
}
}
}
}
// destroy this block
S[tickPx][tickPy] = 0;
//printMap(S, Hp);
} void printInfo(pos p, stBlk pb)
{
printf("p:(%d,%d)\n", get_x_from_pos(p), get_y_from_pos(p));
pos up = get_u_pos_from_blk(pb);
pos dp = get_d_pos_from_blk(pb);
pos lp = get_l_pos_from_blk(pb);
pos rp = get_r_pos_from_blk(pb);
printf("up:(%d,%d)\n", get_x_from_pos(up), get_y_from_pos(up));
printf("dp:(%d,%d)\n", get_x_from_pos(dp), get_y_from_pos(dp));
printf("lp:(%d,%d)\n", get_x_from_pos(lp), get_y_from_pos(lp));
printf("rp:(%d,%d)\n", get_x_from_pos(rp), get_y_from_pos(rp));
}
void updateAllPos(array<array<stBlk, 10>, 10> &state, pos &RealHeadPos)
{
array<array<pos, 10>, 10> temp;
//for (int i=0; i<10; ++i) temp[i].fill(0);
for(int i=0; i<10; ++i) for(int j=0; j<10; ++j) temp[i][j] = OUT_POS; int xx1 = 0;
pos p1 = RealHeadPos;
while (p1 != OUT_POS) {
xx1++;
pos q1 = p1;
int yy1 = 0;
//cout<<"fuck1"<<endl;
while (q1 != OUT_POS) {
//printf("%d\n", RealHeadPos);
yy1++;
//printMap(state, RealHeadPos);
//printf("in p1=(%d,%d) q1=(%d,%d)\n",get_x_from_pos(p1), get_y_from_pos(p1), get_x_from_pos(q1), get_y_from_pos(q1));
//printInfo(p1, state[get_x_from_pos(p1)][get_y_from_pos(p1)]);
//printInfo(q1, state[get_x_from_pos(q1)][get_y_from_pos(q1)]);
temp[xx1-1][yy1-1] = q1;
q1 = get_u_pos_from_blk(state[get_x_from_pos(q1)][get_y_from_pos(q1)]);
}
p1 = get_r_pos_from_blk(state[get_x_from_pos(p1)][get_y_from_pos(p1)]);
//printf("out p1=(%d,%d) q1=(%d,%d)\n",get_x_from_pos(p1), get_y_from_pos(p1), get_x_from_pos(q1), get_y_from_pos(q1));
}
//printf("im out.\n");
for(int x=0; x<10; ++x) {
if (OUT_POS == temp[x][0]) {
break;
}
for(int y=0; y<10; ++y) {
if (OUT_POS == temp[x][y]) {
break;
}
//temp[x][y]--;
if (9 == y) {
set_u_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], OUT_POS);
}
else {
set_u_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], temp[x][y+1]);
}
if (0 == y) {
set_d_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], OUT_POS);
}
else {
set_d_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], temp[x][y-1]);
}
if (0 == x) {
set_l_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], OUT_POS);
}
else {
set_l_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], temp[x-1][y]);
}
if (9 == x) {
set_r_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], OUT_POS);
}
else {
set_r_pos_in_blk(state[get_x_from_pos(temp[x][y])][get_y_from_pos(temp[x][y])], temp[x+1][y]);
}
}
}
return;
} bool checkIsSingle(array<array<stBlk, 10>, 10> &state, pos &ckPos)
{
char ck_x = get_x_from_pos(ckPos);
char ck_y = get_y_from_pos(ckPos); color cx = g_initColorMap[ck_x][ck_y];
stBlk bx = state[ck_x][ck_y]; pos up = get_u_pos_from_blk(bx);
if (up!=OUT_POS && g_initColorMap[get_x_from_pos(up)][get_y_from_pos(up)]==cx){
return false;
}
pos dp = get_d_pos_from_blk(bx);
if (dp!=OUT_POS && g_initColorMap[get_x_from_pos(dp)][get_y_from_pos(dp)]==cx){
return false;
}
pos lp = get_l_pos_from_blk(bx);
if (lp!=OUT_POS && g_initColorMap[get_x_from_pos(lp)][get_y_from_pos(lp)]==cx){
return false;
}
pos rp = get_r_pos_from_blk(bx);
if (rp!=OUT_POS && g_initColorMap[get_x_from_pos(rp)][get_y_from_pos(rp)]==cx){
return false;
} return true;
} int destroy(array<array<stBlk, 10>, 10> &state, pos &realHeadPos, pos realTickPos, array<array<stBlk, 10>, 10> &tmpState, pos &tmpRealHeadPos)
{
pos toTick[105];
int head=0, tail=1;
toTick[tail] = realTickPos; bool vis[257];
for(int i=0; i<257; ++i) vis[i] = 0;
vis[realTickPos] = 1; while (head < tail) {
pos pp = toTick[++head];
char ppx = get_x_from_pos(pp);
char ppy = get_y_from_pos(pp);
color pc = g_initColorMap[ppx][ppy]; stBlk ppb = state[ppx][ppy];
pos up = get_u_pos_from_blk(ppb);
if (up!=OUT_POS && !vis[up] && g_initColorMap[get_x_from_pos(up)][get_y_from_pos(up)]==pc){
vis[up] = 1;
toTick[++tail] = up;
}
pos dp = get_d_pos_from_blk(ppb);
if (dp!=OUT_POS && !vis[dp] && g_initColorMap[get_x_from_pos(dp)][get_y_from_pos(dp)]==pc){
vis[dp] = 1;
toTick[++tail] = dp;
}
pos lp = get_l_pos_from_blk(ppb);
if (lp!=OUT_POS && !vis[lp] && g_initColorMap[get_x_from_pos(lp)][get_y_from_pos(lp)]==pc){
vis[lp] = 1;
toTick[++tail] = lp;
}
pos rp = get_r_pos_from_blk(ppb);
if (rp!=OUT_POS && !vis[rp] && g_initColorMap[get_x_from_pos(rp)][get_y_from_pos(rp)]==pc){
vis[rp] = 1;
toTick[++tail] = rp;
}
}
for(int i=1; i<=tail; ++i) {
tickOneBlk(state, realHeadPos, toTick[i]);
tickOneBlk(tmpState, tmpRealHeadPos, toTick[i]);
}
return tail;
} int explore(array<array<stBlk, 10>, 10> state, pos RealHeadPos)
{
//cout<<RealHeadPos<<endl;
int sco = g_SCORE[state];
if ( sco != 0 ) {
return sco;
} //printMap(state, RealHeadPos);
updateAllPos(state, RealHeadPos); array<array<stBlk, 10>, 10> tmpState = state;
pos tmpRealHeadPos = RealHeadPos;
int tmpMax = 1; while (tmpRealHeadPos != OUT_POS) {
if (checkIsSingle(state, tmpRealHeadPos) == true) {
tickOneBlk(tmpState, tmpRealHeadPos, tmpRealHeadPos);
}
else {
array<array<stBlk, 10>, 10> state1 = state;
pos RealHeadPos1 = RealHeadPos;
pos RealTickPos = tmpRealHeadPos;
int cnt = destroy(state1, RealHeadPos1, RealTickPos, tmpState, tmpRealHeadPos);
tmpMax = max(tmpMax, explore(state1, RealHeadPos1)+5*cnt*cnt);
}
}
g_SCORE[state] = tmpMax;
#if 0
g_mapSize++;
#endif
if(tmpMax > g_BEST_SCORE) {
g_BEST_SCORE = tmpMax;
cout << "---------------------------" << endl;
cout << "Best Score: " << g_BEST_SCORE << endl;
printMap(state, RealHeadPos);
//g_BEST_SCORE = 999999;
}
//printt(state, g_BEST_SCORE);
return tmpMax;
} void proc()
{
g_BEST_SCORE = explore(g_initStateMap, g_initHeadPos);
cout << "BEST SCORE :" << g_BEST_SCORE << endl;
} void random(pos a[], int n)
{
srand((unsigned)time(NULL));
for(int i=0; i<n; ++i) {
int index = rand()%(n-i)+i;
if (index != i) {
pos tmp = a[i];
a[i] = a[index];
a[index] = tmp;
}
}
} void test(array<array<stBlk, 10>, 10> S, pos H)
{
pos tick[100];
for(int i=0; i<100; ++i) {
tick[i] = get_pos_from_xy(i/10, i%10);
}
random(tick, 100);
for(int i=0; i<100; ++i) {
int x; stBlk tB = S[get_x_from_pos(tick[i])][get_y_from_pos(tick[i])];
pos uu = get_u_pos_from_blk(tB);
pos dd = get_d_pos_from_blk(tB);
pos ll = get_l_pos_from_blk(tB);
pos rr = get_r_pos_from_blk(tB);
printf("-----------------------------\n");
printf("tick on (%d, %d)\n", get_x_from_pos(tick[i]), get_y_from_pos(tick[i]));
printf("u:(%d,%d) d:(%d,%d) l:(%d,%d) r:(%d,%d)\n",\
get_x_from_pos(uu), get_y_from_pos(uu),\
get_x_from_pos(dd), get_y_from_pos(dd),\
get_x_from_pos(ll), get_y_from_pos(ll),\
get_x_from_pos(rr), get_y_from_pos(rr)
);
//cin >> x;
tickOneBlk(S, H, tick[i]);
printf("tick Done.\n");
//printMap(S, H);
printf("printMap Done.\n");
}
cout<<"test done"<<endl;
printf("%d\n", H);
//printMap(S, H);
}
int main()
{
globalInit();
readInitMap();
printMap(g_initStateMap, g_initHeadPos);
//test(g_initStateMap, g_initHeadPos);
//tickOneBlk(g_initStateMap, g_initHeadPos, get_pos_from_xy(4, 7));
//printMap(g_initStateMap, g_initHeadPos);
proc(); return 0;
}

v3的更多相关文章

  1. DBImport V3.7版本发布及软件稳定性(自动退出问题)解决过程分享

    DBImport V3.7介绍: 1:先上图,再介绍亮点功能: 主要的升级功能为: 1:增加(Truncate Table)清表再插入功能: 清掉再插,可以保证两个库的数据一致,自己很喜欢这个功能. ...

  2. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  3. atitit 商业项目常用模块技术知识点 v3 qc29

    atitit 商业项目常用模块技术知识点 v3 qc29 条码二维码barcodebarcode 条码二维码qrcodeqrcode 条码二维码dm码生成与识别 条码二维码pdf147码 条码二维码z ...

  4. Atitit. 提升存储过程与编程语言的可读性解决方案v3 qc25.docx

    Atitit. 提升存储过程与编程语言的可读性解决方案v3 qc25.docx 1. 大原则:分解+命名1 1.1. 命名规范1 1.2. 分层.DI和AOP是继OO1 1.3. 运算符可读性一般要比 ...

  5. 高效 Java Web 开发框架 JessMA v3.5.1

    JessMA 是功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hibernate.MyBatis 与 J ...

  6. 高性能 TCP & UDP 通信框架 HP-Socket v3.5.3

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  7. 高性能 TCP & UDP 通信框架 HP-Socket v3.5.2

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  8. 高性能 TCP & UDP 通信框架 HP-Socket v3.5.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  9. 高性能 TCP & UDP 通信框架 HP-Socket v3.4.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  10. 高性能 TCP & UDP 通信框架 HP-Socket v3.3.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

随机推荐

  1. PHP的加密伪随机数生成器的使用

    今天我们来介绍的是 PHP 中的加密伪随机数生成器(CSPRNG 扩展).随机数的生成其实非常简单,使用 rand() 或者 mt_rand() 函数就可以了,但是我们今天说的这个则是使用了更复杂算法 ...

  2. Git报错-refusing to merge unrelated histories

    执行git pull 时报错: 出现这个问题的最主要原因还是在于本地仓库和远程仓库实际上是独立的两个仓库.假如我之前是直接clone的方式在本地建立起远程github仓库的克隆本地仓库就不会有这问题了 ...

  3. redis linux的 安装

    https://blog.csdn.net/u011159417/article/details/80085011 安装: 1.获取redis资源 wget http://download.redis ...

  4. python对象引用和垃圾回收

    变量="标签" 变量a和变量b引用同一个列表: >>> a = [1, 2, 3] >>> b = a >>> a.appen ...

  5. GIS应用|快速搭建REST地图服务

    SuperMap Online云存储作为您的"在线GIS云盘",除了可以在云端存储GIS数据,还可以将数据直接发布多种REST服务,为您节省购买和部署SuperMap iServe ...

  6. jzoj4496-[GDSOI2016]互补约数【莫比乌斯反演】

    正题 题目链接:https://gmoj.net/senior/#main/show/4496 题目大意 给出\(n\),定义 \[f(i)=\sum_{d|i}gcd(d,\frac{i}{d}) ...

  7. P7115-[NOIP2020]移球游戏【构造】

    正题 题目链接:https://www.luogu.com.cn/problem/P7115 题目大意 \(n+1\)个柱子,前面\(n\)个上面各有\(m\)个球,球有\(n\)种颜色,每种\(m\ ...

  8. es相关监控指标梳理

    ###################ElasticSearch监控指标梳理########################### #author:lugh1 # #date:2021-09-26 # ...

  9. mqtt网关服务器连接阿里云关联物模型

    mqtt网关服务器连接阿里云关联物模型 卓岚专门为工业环境设计的RS485设备数据采集器/物联网网关,兼具串口服务器.Modbus网关.MQTT网关.RS485转JSON等多种功能于一体. 可以连接阿 ...

  10. 吴恩达--神经网络-week1-hw4

    # Ref: https://blog.csdn.net/u013733326/article/details/79767169 import numpy as np import testCases ...