wiki-1004-四子连棋 2021-11-15 【wiki-1004-四子连棋】恶心!!!写了好久,调了好久,恶心!!!从中领悟了一个道理“复制有风险,粘贴需谨慎。”(因为程序中有类似的部分,所以就复制粘贴,结果忘记改变量名了,找了好久......555555555555555555555555555) #include #include #include #include #include #include #include #include using namespace std; struct aaa { int x,y; }; struct node { int hash[5][5],step,flag; aaa kb1,kb2; }; queue q; maphh; node start; void init() { freopen("wiki.in","r",stdin); freopen("wiki.out","w",stdout); }string gethash(node st) { string qwer = ""; for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { qwer =qwer + (char)(st.hash[i][j]+'0'); } } return qwer; }bool trytoinsert(node st) { string qqq=gethash(st); if(hh[qqq]) return false; hh[qqq]=1; return true; }void check(node st) { if((st.hash[1][1] == st.hash[2][2] && st.hash[3][3] == st.hash[4][4] && st.hash[2][2] == st.hash[3][3]) || (st.hash[1][4] == st.hash[2][3] && st.hash[2][3] == st.hash[3][2] && st.hash[3][2] == st.hash[4][1])) {printf("%d",st.step); exit(0); } if((st.hash[1][1] == st.hash[1][2] && st.hash[1][2] == st.hash[1][3] && st.hash[1][3] == st.hash[1][4]) || (st.hash[2][1] == st.hash[2][2] && st.hash[2][2] == st.hash[2][3] && st.hash[2][3] == st.hash[2][4])) {printf("%d",st.step); exit(0); } if((st.hash[3][1] == st.hash[3][2] && st.hash[3][2] == st.hash[3][3] && st.hash[3][3] == st.hash[3][4]) || (st.hash[4][1] == st.hash[4][2] && st.hash[4][2] == st.hash[4][3] && st.hash[4][3] == st.hash[4][4])) {printf("%d",st.step); exit(0); } if((st.hash[1][1] == st.hash[2][1] && st.hash[2][1] == st.hash[3][1] && st.hash[3][1] == st.hash[4][1]) || (st.hash[1][2] == st.hash[2][2] && st.hash[2][2] == st.hash[3][2] && st.hash[3][2] == st.hash[4][2])) {printf("%d",st.step); exit(0); } if((st.hash[1][3] == st.hash[2][3] && st.hash[2][3] == st.hash[3][3] && st.hash[3][3] == st.hash[4][3]) || (st.hash[1][4] == st.hash[2][4] && st.hash[2][4] == st.hash[3][4] && st.hash[3][4] == st.hash[4][4])) {printf("%d",st.step); exit(0); } }void readdata() { char qq[10][10]; int cnt=0; for(int i=0; i<4; i++) { scanf("%s",qq[i]); for(int j=0; j<4; j++) { if(qq[i][j]=='B') start.hash[i+1][j+1]=1; if(qq[i][j]=='W') start.hash[i+1][j+1]=2; if(qq[i][j]=='O') { start.hash[i+1][j+1]=0; if(!cnt) { start.kb1.x=i+1; start.kb1.y=j+1; cnt++; } else { start.kb2.x=i+1; start.kb2.y=j+1; } } } } start.flag=1; start.step=0; check(start); if(trytoinsert(start)) q.push(start); start.flag=2; q.push(start); }int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; void work() { while(!q.empty()) { node cur=q.front(); q.pop(); for(int i=0; i<4; i++) { node st,sd; memcpy(&st,&cur,sizeof(cur)); memcpy(&sd,&cur,sizeof(cur)); int x1=st.kb1.x,y1=st.kb1.y,x2=st.kb2.x,y2=st.kb2.y; int newx1=x1+dx[i],newy1=y1+dy[i]; while(newx1 > 0 && newx1 < 5 && newy1 > 0 && newy1 < 5 && st.hash[newx1][newy1]!=0) { if((st.hash[newx1][newy1]==1 && st.flag==1) || (st.hash[newx1][newy1]==2 && st.flag==2)) break; if(st.hash[newx1][newy1]==1) st.flag=1; else st.flag=2; st.hash[x1][y1] = st.hash[newx1][newy1]; st.hash[newx1][newy1]=0; st.kb1.x=newx1,st.kb1.y=newy1; st.step++; check(st); if(trytoinsert(st)) q.push(st); break; } int newx2=x2+dx[i],newy2=y2+dy[i]; while(newx2 > 0 && newx2 < 5 && newy2 > 0 && newy2 < 5 && sd.hash[newx2][newy2]!=0) { if((sd.hash[newx2][newy2]==1 && sd.flag==1) || (sd.hash[newx2][newy2]==2 && sd.flag==2)) break; if(sd.hash[newx2][newy2]==1) sd.flag=1; else sd.flag=2; sd.hash[x2][y2]=sd.hash[newx2][newy2]; sd.hash[newx2][newy2]=0; sd.kb2.x=newx2,sd.kb2.y=newy2; sd.step++; check(sd); if(trytoinsert(sd)) q.push(sd); break; } } } }int main() { init(); readdata(); work(); return 0; } 推荐阅读 你有什么给正在减肥的人的忠告? 为什么年轻人不要玩核桃 万象物语挑战模式t13打法攻略 万象物语挑战模式怎么打 西瓜鹌鹑汤 算法分析动态规划,动态规划背包问题算法分析 女生古铜色皮肤穿什么 hpv|世卫组织官员和中国医学专家呼吁尽早接种HPV疫苗,有必要吗? 虾池养殖鲈鱼技术 虾池钓鲈鱼方法 板栗怎样做辅食 值得一看的短句 access成绩分析er图,根据access创建er图 科比拿过连续9场40吗 体内氨基酸脱氨基的主要方式是 体内氨基酸脱氨基的主要方式是什么 安卓默认时钟字体,如何在智能手机上修改笔记字体? spss怎么交叉分析法,交叉分析法怎么做 go语言关键字 golang go关键字 lightdm 分析,ubuntu安装lightdm 如何让孩子变得自信和勇敢 如何让胆小孩子变勇敢 旅游app竞争者分析,旅游APP需求分析 城市建设开发与古建筑的保护,如何平衡城市发展和文化遗产保护 2018年11月19日|2018年11月19日 星期一 亲子日记第144篇 遇到一哭二闹三打滚的孩子,怎么办┃山伯教育 这辈子我们都不要再联系了 跌跌撞撞奔向你|跌跌撞撞奔向你 第四章(你补英语,我补物理) 奔向你的城市 2019年12月24日 Ⅴ爱阅读,亲子互动——打卡第178天 眼观耳听美食的日子 子龙老师语录 成交的种子咖啡冥想