蚂蚁相撞问题实例(一)

2013-05-14 09:23:40 · 作者: · 浏览: 706

  过程中两个思想:1、两只蚂蚁相撞后假设互穿而过得最终各蚂蚁位置;2、终态各蚂蚁的相对位置不变,与始态相同。

  [cpp]

  #include

  #include

  using namespace std;

  const int maxn = 10000 + 10;

  struct ant //定义蚂蚁数据类型

  {

  int id; //输入时的id编号,从0开始计数

  int pos; //距左端的位置

  int dir; //方向

  bool operator < (const ant &a) const //重载运算符,按位置前后为序

  {

  return pos < a.pos;

  }

  };

  ant before[maxn], after[maxn]; //未处理前的before,处理后为after

  const char dir_name[] = {"L", "Turning", "R"}; //方向数组

  int order[maxn]; //输入的第i只蚂蚁是位置上的第order[i]只

  int main()