学习心得(code)

superlong@CoreCoder

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  74 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

公告

文字可能放在http://blog.csdn.net/superlong100,此处存放代码

常用链接

留言簿(4)

我参与的团队

搜索

  •  

最新随笔

最新评论

  • 1. re: Poj 1279
  • 对于一个凹多边形用叉积计算面积 后能根据结果的正负来判断给的点集的时针方向?
  • --bsshanghai
  • 2. re: Poj 3691
  • 你写的这个get_fail() 好像并是真正的get_fail,也是说fail指向的串并不是当前结点的子串。为什么要这样弄呢?
  • --acmer1183
  • 3. re: HDU2295[未登录]
  • 这个是IDA* 也就是迭代加深@ylfdrib
  • --superlong
  • 4. re: HDU2295
  • 评论内容较长,点击标题查看
  • --ylfdrib
  • 5. re: HOJ 11482
  • 呵呵..把代码发在这里很不错..以后我也试试...百度的编辑器太烂了....
  • --csuft1

阅读排行榜

评论排行榜

#include <stdio.h>
#include 
<memory.h>

int  n, m, l, test;
int  mv[4][2= {{0-1}, {-10}, {01}, {10}, };//left up right down
int  pow[9= {01416642561024409616384};//for 4's pow

bool tu[21][21];
int  mym[21][21][65535];
int  f[21][21];

struct nod
{
    
int x, y, s, cost, step;
};

bool operator <(nod a, nod b)
{
    
return a.x < b.x || a.x == b.x && a.y < b.y ||
        a.x 
== b.x && a.y == b.y && a.s < b.s;
}

int si, sJ;
nod ini;

int cha(int x, int y)
{
    
if(x < 0return 1;
    
if(x > 0return 3;
    
if(y < 0return 0;
    
if(y > 0return 2;
}

void read()
{
    
int i, x, y, tx, ty;
    scanf(
"%d %d"&si, &sJ);
    x 
= si; y = sJ;
    
int ch = 0;
    
for(i = 0; i < l - 1; i ++)
    {
        scanf(
"%d %d"&tx, &ty);
        ch 
= ch * 4 + cha(tx - x, ty - y);
        x 
= tx; 
        y 
= ty;
    }
    ini.s 
= ch;
    ini.x 
= si; ini.y = sJ;
    
int stone;
    memset(tu, 
0sizeof(tu));
    scanf(
"%d"&stone);
    
for(i = 0; i < stone; i ++)
    {
        scanf(
"%d %d"&x, &y);
        tu[x][y] 
= 1;
    }
}

int open, close, first;
nod q[
1000001];

bool ok(nod t, int index)
{
    
short dx = t.x + mv[index][0], dy = t.y + mv[index][1];
    
short x, y;;
    x 
= t.x;
    y 
= t.y;
    
for(int i = 0; i < l - 1; i ++)
    {
        x 
= x + mv[ t.s / pow[l - 1 - i] ][0];
        y 
= y + mv[ t.s / pow[l - 1 - i] ][1];
        
if(x == dx && y == dy) return false;
        t.s 
= t.s % pow[l - 1 - i];
    }
    
return true;
}

void change(int &ch, int i)
{
    ch 
= ch >> 2;
    ch 
+= ((i + 2% 4* pow[l - 1];
}

bool check(nod t)
{
    
short xx = t.x, yy = t.y;
    
if(xx < 1 || xx > n || yy < 1 || yy > m) return false;
    
if(tu[xx][yy]) return false;
    
return true;
}

void heap(nod tp)
{
    nod tmp;
    tmp 
= q[++open] = tp;
    
int i = open, J = i / 2;
    
while( i > first)
    {
        
if( tmp.cost < q[J].cost && J > first)
        {
            q[i] 
= q[J];
            i 
= J;
            J 
= i / 2;
        }
        
else 
            
break;
    }
    q[i] 
= tmp;
}

bool expend(nod temp)
{
    nod t;
    
for(int i = 0; i < 4; i ++)
    
if(ok(temp, i))
    {
        t 
= temp;
        t.x 
+= mv[i][0];
        t.y 
+= mv[i][1];
        t.step 
++;
        
if(t.x == 1 && t.y == 1return true;
        change(t.s, i); 
        
if(check(t) && mym[t.x][t.y][t.s] != test)
        {
            mym[t.x][t.y][t.s] 
= test;
            t.cost 
= f[t.x][t.y] + t.step;
            heap(t);
        }
    }
    
return false;
}

int bfs()
{
    nod temp;
    close 
= -1;    open = 0;
    q[
0= ini;
    q[
0].cost = f[ini.x][ini.y];
    q[
0].step = 0;
    
if(ini.x == 1 && ini.y == 1return 0;
    mym[ini.x][ini.y][ini.s] 
= test;
    
int size;
    
while(close < open)
    {
        temp 
= q[++ close];
        first 
= close + 1;
        
if(expend(temp)) return temp.step + 1;
    }
    
return -1;
}

struct td{int x, y;}que[1001];
bool ha[21][21];
void pre()
{
    close 
= -1, open = 0;
    memset(ha,
0,sizeof(ha));
    td t;
    que[
0].x = 1;
    que[
0].y = 1;
    ha[
1][1= 1;
    f[
1][1= 0;
    
while(close < open)
    {
        t 
= que[++close];
        
for(int i = 0; i < 4; i ++)
        {
            
int x = t.x + mv[i][0], y = t.y + mv[i][1];
            
if(x < 1 || x > n || y < 1 || y > m) continue;
            
if(ha[x][y] || tu[x][y]) continue;
            ha[x][y] 
= 1;
            f[x][y] 
= f[t.x][t.y] + 1;
            que[
++open].x = x;
            que[open].y 
= y;
        }
    }
}

int main()
{
    freopen(
"in.txt","r",stdin);
    test 
= 0;
    
while(scanf("%d %d %d"&n, &m, &l), n||m||l)
    {
        read();
        pre();
        
++test;
        printf(
"Case %d: %d\n",test,bfs());
    }
    
while(1);
}

posted on 2009-09-01 19:18 superlong 阅读(158) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理