学习心得(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 <iostream>
#define H 12
using namespace std;

int movem[8][2= {{-2,1}, {-1,2}, {1,2}, {2,1}, 
                  {
2,-1}, {1,-2}, {-1,-2}, {-2,-1}};
int move_m[8][2= {{-1,0}, {0,1}, {0,1}, {1,0}, {1,0}, {0,-1}, {0,-1}, {-1,0}};
int move[4][2= {{01}, {0-1}, {10}, {-10}};
bool h[150][150][150];
char map[11][11];
int  nn, mm;
int  ci, cj, mi, mj, pi, pj, si, sj;

struct node
{
    
int c, m, p, step;
}que[
1000001];

int  open, close;
int  c, m, p, ans;
node t, next;

int hash(int a,int b)
{
return a * H + b;}

void read_map()
{
    
int i, j;
    getchar();
    
for(i = 0; i < nn; i ++) gets(map[i]);
    ci 
= cj = mi = mj = pi = pj = -1;
    
for(i = 0; i < nn; i ++)
    
for(j = 0; j < mm; j ++)
    {
        
if(map[i][j] == 'C')
        {ci 
= i; cj = j; map[i][j] = '.';}
        
if(map[i][j] == 'P')
        {pi 
= i; pj = j; map[i][j] = '.';}
        
if(map[i][j] == 'M')
        {mi 
= i; mj = j; map[i][j] = '.';}
        
if(map[i][j] == 'S')
        {si 
= i; sj = j;}
    }
}

bool check_c(node st, int x, int y)
{
    
if(x<0 || y<0 || x>=nn || y>=mm) return false;
    
if(h[st.c][st.m][st.p]) return false;
    
if(map[x][y] == 'D' || hash(x, y) == st.m || hash(x, y) == st.p) return false;
    
return true;
}

bool isok(int x, int y)
{
    
if(x==si && y==sj) return true;
    
return false;
}

void ex_c()
{
    next 
= t;    next.step ++;
    
int x, y, tx, ty;
    x 
= next.c / H;        y = next.c % H;
    
for(int i = 0; i < 4; i ++)
    {
        tx 
= x + move[i][0];
        ty 
= y + move[i][1];
        next.c 
= hash(tx, ty);
        
while(check_c(next, tx, ty))
        {
            
//printf("ju: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
            if(isok(tx, ty))
            {
                ans 
= next.step;
                
return;
            }
            h[next.c][next.m][next.p] 
= 1;
            que[
++open] = next;
            tx 
+= move[i][0];
            ty 
+= move[i][1];
            next.c 
= hash(tx, ty);
        }
    }
}

bool check_for_m(int x, int y, node st, int key)
{
    
int xx = x+move_m[key][0], yy = y+move_m[key][1];
    
//撇马脚 
    if(xx < 0 || yy < 0 || xx >= nn || yy >= mm) return false;
    
if(map[xx][yy] == 'D' || hash(xx, yy) == st.c || hash(xx, yy) == st.p || hash(xx, yy) == hash(si, sj))
        
return false;
    xx 
= st.m / H;
    yy 
= st.m % H;
    
if(xx < 0 || xx >= nn || yy < 0 || yy >= mm ) return false;
    
if(st.m == st.c || st.m == st.p || map[xx][yy] == 'D' || h[st.c][st.m][st.p]) return false;
    
return true;
}

void ex_m()
{
    next 
= t;    next.step ++;
    
int x, y, tx, ty;
    x 
= next.m / H;        y = next.m % H;
    
for(int i = 0; i < 8; i ++)
    {
        tx 
= x + movem[i][0];    ty = y + movem[i][1];
        next.m 
= hash(tx, ty);
        
if(check_for_m(x, y, next, i))
        {
            
//printf("ma: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
            if(isok(tx, ty))
            {
                ans 
= next.step;
                
return;
            }
            h[next.c][next.m][next.p] 
= 1;
            que[
++open] = next;
        }
    }
}

bool isok_p(node st)
{
    
int x, y, inc, cnt;
    x 
= st.p / H;    y = st.p % H;
    
if(x == si)
    {
        cnt 
= 0; inc = 1;
        
if(y > sj) inc = -1;
        
for(int j = y;; j += inc)
        {
            
if(map[x][j] == 'D' || hash(x, j) == st.c || hash(x, j) == st.m)
                cnt 
++;
            
if(j == sj) break;
        }
        
if(cnt == 1return true;
        
return false;
    }
    
if(y == sj)
    {
        cnt 
= 0; inc = 1;
        
if(x > si) inc = -1;
        
for(int i = x;; i += inc)
        {
            
if(map[i][y] == 'D' || hash(i, y) == st.c || hash(i, y) == st.m)
                cnt 
++;
            
if(i == si) break;
        }
        
if(cnt == 1return true;
        
return false;
    }
    
return false;
}

bool check_p(node st, int x, int y)
{
    
if(x<0 || y<0 || x>=nn || y>=mm) return false;
    
if(h[st.c][st.m][st.p]) return false;
    
if(map[x][y] == 'D' || hash(x, y)==st.m || hash(x, y)==st.c) return false;
    
if(hash(x, y) == hash(si, sj)) return false;
    
return true;
}

void ex_p()
{
    next 
= t; next.step ++;
    
int x, y, tx, ty;
    x 
= next.p / H;        y = next.p % H;
    
if(isok_p(next))
    {
        ans 
= next.step;
        
return;
    }
    
for(int i = 0; i < 4; i ++)
    {
        tx 
= x + move[i][0];
        ty 
= y + move[i][1];
        next.p 
= hash(tx, ty);
        
while(check_p(next, tx, ty))
        {
            
//printf("ju: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
            h[next.c][next.m][next.p] = 1;
            que[
++open] = next;
            tx 
+= move[i][0];
            ty 
+= move[i][1];
            next.p 
= hash(tx, ty);
        }
    }
}

void bfs()
{
    open 
= 0; close = -1;
    c 
= hash(ci, cj);    m = hash(mi, mj);    p = hash(pi, pj);
    memset(h,
0,sizeof(h));
    h[c][m][p] 
= 1;
    que[
0].c = c;    que[0].m = m;    que[0].p = p;
    que[
0].step = 0;
    
while(close < open)
    {
        t 
= que[++close];
        ex_c();    
if(ans > 0return;
        ex_m();    
if(ans > 0return;
        ex_p();    
if(ans > 0return;
    }
}

int main()
{
    
//freopen("in.txt","r",stdin);
    while(scanf("%d %d"&nn, &mm) != EOF)
    {
        ans 
= 0;
        read_map();
        bfs();
        
if(ans > 0) printf("%d\n",ans);
        
else        puts("OH!That's impossible!");
        puts(
"");
    }
    
//while(1);
}


posted on 2009-08-18 15:56 superlong 阅读(84) 评论(0)  编辑 收藏 引用

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