1.4 The Clocks

思路基本上是看看题解自己想想出来的。

通过这题自己以后应该能对位运算加速有一些印象,逐步积累技巧,毕竟我是看着位运算的标题先自己想了很久而又再看题解恍然大悟的。

顺便也第一次写bfs(bfs+位运算)。  第一次写。。而且是两者,所以存在很大的困难。。这不代码还没搞出来,每一个细节都要自己专研。

 

再说枚举算法,看样子是说枚举每一种可能,然后比较出最小步骤吧。就这题来说,dfs和枚举有什么差别?不都是要遍历所以的节点,况且dfs还要考虑会不会爆栈,手工栈。希望自己的见解是正确的,可惜没找到人交流。没有办法,自己可以说没有写过dfs,bfs,脑子里的确有些混淆。

 

后面一些规律性优化的算法?不准备看了。时间比较少。

 

对于这道题,想的很多,比packing rectangles还多。兴许是受物理老师的影响,强调去悟,发散,而不是就提论题。希望通过这道题能加深(了解)bfs,dfs,位运算。

大致的代码如下,差bfs

/*
ID:y7276571
LANG: C++
TASK: clocks
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#define REP(i,n) for(int i = 0; i < n; i++)
using namespace std;
const long move[9] = {18911232, 19136512, 2363904, 16810048, 2134536, 262657, 36936, 73, 4617};
long state[262144] = {0};
void bfs()
{
string lis;
long int front = 0, rear = 1;
while(front != rear)
{
REP(i,n)
{

}
}
}
int main()
{
freopen("clocks.in", "r", stdin);
// freopen("clocks.out", "w", stdout);
long int a = 1 << 24;
cout << state[0] << endl;
REP(i,9)
{
int temp;
cin >> temp;
switch(temp)
{
case 12: temp = 0; break;
case 3: temp = 1; break;
case 6: temp = 2; break;
case 9: temp = 3; break;
}
state[0] += (temp << (8-i)*3);
}
bfs();
cout << state[0] << endl;
return 0;
}

 --------------------------------------------------------------------------------------------------------------------

奋斗到现在..18日下午2:40

总算是搞定了这道题

实际上位运算是和枚举搭配的....bfs是搭配hash........

我确是bfs+位运算!!!!

先用string写 写到今天才发现

必须要开两个存状态的数组.前者用于不断move,后者则是记录move之后的状态,可以看作hash?

因为如果不开后者,前者最大数据乃是2^27-1,开不到那么大的bool.

题中每个种的状态是用2bit就可以存下的...所以实际上最大2^19-1存下?bool数组可以开大..

所以每次move计算出state之后,再用计算出的state计算wocao(0 0),进行判重.

 

废话多..实际上看代码很容易看懂的..

关于常数:524287 (011 011 011.......011)得到

     262144 4^9

 

/*
ID:y7276571
LANG: C++
TASK: clocks
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstring>
#define REP(i,n) for(int i = 0; i < n; i++)
using namespace std;
const long move[9] = {18911232, 19136512, 2363904, 16810048, 2134536, 262657, 36936, 73, 4617};
long state[262144] = {0}, wocao[262144] = {0};
char lis[262144][30];
bool isstore[524287] = {false};
void bfs()
{
long int front = 0, rear = 1;
bool ok = false;
isstore[wocao[0]] = true;
while(!ok)
{
REP(i,9)
{
state[rear] = (state[front]+move[i])&57521883;
REP(j,9)
wocao[rear] += ((state[rear] >> (8-j)*3) & 3) << (8-j)*2;
if(isstore[wocao[rear]]) { wocao[rear] = 0; continue; }
strcpy(lis[rear], lis[front]); lis[rear][strlen(lis[rear])] = i+49;
isstore[wocao[rear]] = true;
if(!state[rear]) { ok = true; break; }
rear++;
}
front++;
}
int len = strlen(lis[rear]);
REP(i,len) i ? cout << " " << lis[rear][i] : cout << lis[rear][i];
cout << endl;
}
int main()
{
freopen("clocks.in", "r", stdin);
freopen("clocks.out", "w", stdout);
REP(i,9)
{
int temp;
cin >> temp;
switch(temp)
{
case 12: temp = 0; break;
case 3: temp = 1; break;
case 6: temp = 2; break;
case 9: temp = 3; break;
}
state[0] += (temp << (8-i)*3);
wocao[0] += (temp << (8-i)*2);
}
bfs();
return 0;
}

藐视我的最多的不超过0.1s~~

不过是渣方法...


Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 13292 KB] Test 2: TEST OK [0.000 secs, 13292 KB] Test 3: TEST OK [0.011 secs, 13292 KB] Test 4: TEST OK [0.022 secs, 13292 KB] Test 5: TEST OK [0.032 secs, 13292 KB] Test 6: TEST OK [0.076 secs, 13292 KB] Test 7: TEST OK [0.076 secs, 13292 KB] Test 8: TEST OK [0.097 secs, 13292 KB] Test 9: TEST OK [0.086 secs, 13292 KB] All tests OK.

Your program ('clocks') produced all correct answers! This is your submission #6 for this problem. Congratulations!

原文链接: https://www.cnblogs.com/shixuehunk/archive/2011/12/17/2291073.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    1.4 The Clocks

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/38756

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午3:22
下一篇 2023年2月8日 下午3:23

相关推荐