您的位置:首页 > 其它

hdu1495 突然一下记起ID和密码。。。既然进来了,,就贴一个吧。。。

2012-07-19 16:46 253 查看
#include <iostream>
#include <queue>
using namespace std;
int s[4];
int a[4];
const int mo[6][2] = {1,2,1,3,2,1,2,3,3,1,3,2};
bool mark[111][111];
int ans;

struct node
{
 int s, m, n, step;
}x;

bool bfs()
{
 queue<node> q;
 int h = s[1] / 2;
 node u, v;
 q.push(x);
 memset(mark, true, sizeof(mark));
 mark[0][0] = false;
 bool flag  = false;
 while (!q.empty())
 {
  u = q.front();
  q.pop();
  if (u.s==h && u.m==h) flag = true;
  if (u.s==h && u.n==h) flag = true;
  if (u.n==h && u.m==h) flag = true;
  if (flag)
  {
   ans = u.step;
   return true;
  }
  for (int i=0;i<6;i++)
  {
   a[1] = u.s;
   a[2] = u.m;
   a[3] = u.n;
   int w = mo[i][0];
   int e = mo[i][1];
   if (a[w]==0) continue;
   if (a[w]+a[e] > s[e])
   {
    int t = s[e] - a[e];
    a[e] = s[e];
    a[w] -= t;
   }
   else
   {
    a[e] += a[w];
    a[w] = 0;
   }
   if (mark[a[2]][a[3]])
   {
    v.s = a[1];
    v.m = a[2];
    v.n = a[3];
    v.step = u.step+1;
    mark[a[2]][a[3]] = false;
    q.push(v);
   }
  }
 }
 return false;
}

int main()
{
 while (scanf("%d%d%d", &s[1], &s[2], &s[3]), s[1])
 {
  if (s[1]%2==1)
  {
   cout<<"NO\n";
   continue;
  }
  x.s = s[1]; x.m = 0; x.n = 0; x.step = 0;
  if (bfs())
    cout<<ans<<endl;
  else
    cout<<"NO\n";
 }
 return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐