刚开始的时候,在bfs里扩展没写好,一直tle。杯具!
#include<iostream> #include<cstdio> #include<string> using namespace std; bool vis[70000]; char pil[5][5]; int bin[5][5],sta[5][5],step[70000][2]; bool txt[5][5]; class node { public: int st; int p; int x,y; int father; }; node queue[70000]; int solve(int end) { int i,j,k,sum,star; sum=0; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { sum+=sta[i][j]*bin[i][j]; } for(i=0;i<=65535;i++) vis[i]=true; vis[sum]=false; star=sum; int front=0,rear=0; rear++; queue[rear].st =sum; queue[rear].p =0; queue[rear].father =-1; while(front!=rear) { front++;sum=queue[front].st ; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { sta[i][j]=sum%2; sum/=2; } for(i=1;i<=4;i++) for(j=1;j<=4;j++) { sum=0 ; for(k=1;k<=4;k++) sum+=((sta[i][k]^1)-sta[i][k])*bin[i][k]; for(k=1;k<=4;k++) if(k!=i) sum+=((sta[k][j]^1)-sta[k][j])*bin[k][j]; sum+=queue[front].st ; if(sum>=0&&vis[sum]) { vis[sum]=false; rear++; queue[rear].father =front; queue[rear].p =queue[front].p +1; queue[rear].x =i;queue[rear].y =j; queue[rear].st =sum; if(sum==end) { printf("%d\n",queue[rear].p ); int cnt=0; k=rear; while(queue[k].father!=-1 ) { step[cnt][0]=queue[k].x;step[cnt][1]=queue[k].y; cnt++; k=queue[k].father ; } for(k=cnt-1;k>=0;k--) printf("%d %d\n",step[k][0],step[k][1]); return 0; } } } } } int main() { int i,j; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { cin>>pil[i][j]; if(pil[i][j]=='-') sta[i][j]=1; else sta[i][j]=0; } int k=1; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { bin[i][j]=k; k*=2; } solve(65535); }
您还没有登录,请您登录后再发表评论
北大POJ2965-The Pilots Brothers' refrigerator 解题报告+AC代码
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
c表示有多少种珍珠 ai 表示第i种珍珠所需的数量 pi 表示第i种珍珠的价钱 每买一种珍珠都需要付额外的10 * pi的钱,便宜的珍珠可以用贵的珍珠来代替,求最少的钱的总数。
POJ 是“北京大学程序在线评测系统”(Peking University Online Judge)的缩写,是个提供编程题目的网站,兼容Pascal、C、C++、Java、Fortran、Python等多种语言。可以按照分类,在POJ上做题。
POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度
北大POJ1027-The Same Game 解题报告+AC代码
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're ...
北大POJ1163-The Triangle 解题报告+AC代码
北大POJ1163-The Triangle
POJ2635-The Embarrassed Cryptographer 测试数据。 来源:NCPC 2005 问题D
北大POJ3267-The Cow Lexicon
北大POJ2635-The Embarrassed Cryptographer 解题报告+AC代码
北大POJ3982-The Fibonacci sequence 解题报告+AC代码
北大POJ3267-The Cow Lexicon 解题报告+AC代码
poj 3548 Restoring the digits.md
poj 3554 Almost the shortest route.md
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
poj 1611 The Suspects 代码 并查集的应用
相关推荐
北大POJ2965-The Pilots Brothers' refrigerator 解题报告+AC代码
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
c表示有多少种珍珠 ai 表示第i种珍珠所需的数量 pi 表示第i种珍珠的价钱 每买一种珍珠都需要付额外的10 * pi的钱,便宜的珍珠可以用贵的珍珠来代替,求最少的钱的总数。
POJ 是“北京大学程序在线评测系统”(Peking University Online Judge)的缩写,是个提供编程题目的网站,兼容Pascal、C、C++、Java、Fortran、Python等多种语言。可以按照分类,在POJ上做题。
POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度
北大POJ1027-The Same Game 解题报告+AC代码
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're ...
北大POJ1163-The Triangle 解题报告+AC代码
北大POJ1163-The Triangle
POJ2635-The Embarrassed Cryptographer 测试数据。 来源:NCPC 2005 问题D
北大POJ3267-The Cow Lexicon
北大POJ2635-The Embarrassed Cryptographer 解题报告+AC代码
北大POJ3982-The Fibonacci sequence 解题报告+AC代码
北大POJ3267-The Cow Lexicon 解题报告+AC代码
poj 3548 Restoring the digits.md
poj 3554 Almost the shortest route.md
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
poj 1611 The Suspects 代码 并查集的应用