花一天时间写的一个连连看,唉!分支限界有的关键点,还是不是掌握的很清楚,居然搞那么长时间,应该
在3个小时之内轻松拿下的,加油了
// MyLinkup.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//grid's intial size
const int nSize = 8;
struct Point{int x;
int y;
Point(){this->x = INT_MAX;this->y = INT_MAX;
}
bool operator==(Point x){return this->x==x.x && this->y==x.y;
}
};//grid point
struct GridPoint{Point c;
int Len;
int Cross;
//Cross is definitely ascending order, how about Len?
//Dijkstra algorithm
GridPoint(){this->Len = INT_MAX;this->Cross = INT_MAX;
}
int operator<(GridPoint item){if(this->Cross < item.Cross)return true;if(this->Cross > item.Cross)return false;//==return this->Len < item.Len;
}
int operator>(GridPoint item){if(this->Cross > item.Cross)return true;if(this->Cross < item.Cross)return false;//==return this->Len > item.Len;
}
};
//ms solution, keep to backtrack
struct MinPoint{int MinCross;
double MinLen;
MinPoint(){this->MinCross = INT_MAX;this->MinLen = DBL_MAX;
}
};
//implementation
int** Intial(int** grid, const int type, const int count, vector<Point>& _pre);
void Linkup(int** grid, vector<Point>& _pre, const int type, const int count);
bool FindPath(int** grid, const int type, const int count, Point start, Point end);
void PrintOut(int** grid);
double dis(int x, int y, int x1, int y1){return abs( x - x1 ) + abs( y - y1 );
}
int** Intial(int** grid, const int type, const int count, vector<Point>& _pre){int* v = new int[type]();
for(int i = 0; i < type; i++)v = count;
//rand() for select abitrary type
srand((unsigned)time(0));
int RANGE_MIN = 0, RANGE_MAX = type;//end can't reach
int POINT_MIN = 0, POINT_MAX = nSize;
for(int total = count * type; total > 0; ){//select the typeint ctype = 0;for(ctype = ( (double)rand() / (double)RAND_MAX ) * RANGE_MAX + RANGE_MIN;;){if(v[ctype] != 0)break;else{ctype = ( (double)rand() / (double)RAND_MAX ) * RANGE_MAX + RANGE_MIN;}}//for[j]Point n;n.x = ( (double)rand() / (double)RAND_MAX ) * POINT_MAX + POINT_MIN;n.y = ( (double)rand() / (double)RAND_MAX ) * POINT_MAX + POINT_MIN;if(find(_pre.begin(), _pre.end(), n)==_pre.end()){//can't findgrid[n.x][n.y] = ctype + 1;v[ctype]--;total--;_pre.push_back(n);}
}
return grid;
}void Linkup(int** grid, vector<Point>& _pre, const int type, const int count){while(_pre.size() > 0){//enter the logicint pair = 0, x = 0, y = 0;Point start, end;cout<<endl;while((pair += scanf("%d,%d", &x, &y)) <= 4){fflush(stdin);if(x>-1&&x<nSize&&y>-1&&y<nSize&&grid[x][y]){if(pair==2){start.x = x;start.y = y;}else{if(start.x != x || start.y != y){end.x = x;end.y = y;break;}else{pair -= 2;cout<<" start mustn't be equal with end"<<endl;}}}else{pair -= 2;cout<<" invalid"<<endl;}}//get valid start and end positionif(grid[start.x][start.y] == grid[end.x][end.y]){//check if find the found or notif(FindPath(grid, type, count, start, end)){//erase i and jvector<Point>::iterator i = find(_pre.begin(), _pre.end(), start);if(i!=_pre.end())_pre.erase(i);vector<Point>::iterator j = find(_pre.begin(), _pre.end(), end);if(j!=_pre.end())_pre.erase(j);//erase the current pointgrid[start.x][start.y] = 0;grid[end.x][end.y] = 0;PrintOut(grid);}elsecout<<" no path to reach"<<endl;}elsecout<<" not equal"<<endl;
}
}bool FindPath(int** grid, const int type, const int count, Point start, Point end){//offset
Point offset[4];
offset[0].x = -1; offset[0].y = 0; //left
offset[1].x = 1; offset[1].y = 0; //right;
offset[2].x = 0; offset[2].y = -1; //up
offset[3].x = 0; offset[3].y = 1;//down
int NumofNbrs = 4;
//step information structure
GridPoint here, nbr;
here.Len = 0;
here.c = start;
here.Cross = -1;
//backtrack array
MinPoint** track = new MinPoint*[nSize]();
for(int i = 0; i < nSize; i++)track = new MinPoint[nSize]();
track[start.x][start.y].MinCross = -1;
track[start.x][start.y].MinLen = 0;
//previous step information
Point** pre = new Point*[nSize]();
for(int i = 0; i < nSize; i++)pre = new Point[nSize]();
pre[start.x][start.y].x = -1;
pre[start.x][start.y].y = -1;
//if we have step the grid or not?
bool** step = new bool*[nSize]();
for(int i = 0; i < nSize; i++)step = new bool[nSize]();
MinHeap<GridPoint> Q;
Q.Insert(here);
while(Q.size() > 0){here = Q.DeleteMin();  //judge path  for(int i = 0 ; i < NumofNbrs; i++){//offsetint x = here.c.x + offset.x;int y = here.c.y + offset.y;for(;x>-1&&x<nSize&&y>-1&&y<nSize; x += offset.x, y += offset.y){nbr.c.x = x;nbr.c.y = y;if(track[x][y].MinCross > track[here.c.x][here.c.y].MinCross + 1 ||( track[x][y].MinCross == track[here.c.x][here.c.y].MinCross + 1 && track[x][y].MinLen > track[here.c.x][here.c.y].MinLen + dis(x, y, here.c.x, here.c.y) ) ){//update informationtrack[x][y].MinCross = track[here.c.x][here.c.y].MinCross + 1;if(track[x][y].MinLen > track[here.c.x][here.c.y].MinLen + dis(x, y, here.c.x, here.c.y))track[x][y].MinLen = track[here.c.x][here.c.y].MinLen + dis(x, y, here.c.x, here.c.y); pre[x][y].x = here.c.x;pre[x][y].y = here.c.y;}if(grid[x][y]!=0){//first non-space, break;break;}else{if(!step[x][y]){//set the priority informationnbr.Cross = track[x][y].MinCross;nbr.Len = track[x][y].MinLen;//set step informationstep[x][y] = true;Q.Insert(nbr);}}}if(nbr.c == end)break;}//forif(nbr.c == end)break;
}
cout<<nbr.c.x<<","<<nbr.c.y<<" "<<nbr.Len<<"[Len] "<<nbr.Cross<<"[Cross] "<<endl;
if(nbr.c.x == end.x && nbr.c.y == end.y && nbr.Cross <= 3){//print pathcout<<" Path : ";for(int x = nbr.c.x, y = nbr.c.y; x != -1 && y != -1;){cout<<"("<<x<<","<<y<<")";int px = pre[x][y].x;int py = pre[x][y].y;x = px;y = py;if(x!=-1&&y!=-1)cout<<" <- ";else break;}cout<<endl;return true;
}
return false;
}
void PrintOut(int** grid){for(int i = 0; i < nSize; i++){for(int j = 0; j < nSize; j++){if(grid[j]==0)cout<<"  ";else cout<<grid[j]<<" ";}cout<<"| x = "<<i<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{//initialization for grid
int** grid = new int*[nSize]();
for(int i = 0; i < nSize; i++)grid = new int[nSize]();
vector<Point> _pre;
//call intial
grid = Intial(grid, 5, 4, _pre);
PrintOut(grid);
Linkup(grid, _pre, 5, 4);
QUIT();
return 0;
}

故人的一份连连看代码,c语言版本相关推荐

  1. qcloud apigateway hmac鉴权代码-go语言版本

    启动命令 编译 go build 执行 ./qcloud-apigateway-sign-demo-go 成功打印结果 x-date: Tue, 15 May 2018 03:48:52 GMT so ...

  2. java连连看倒计时_java连连看代码

    连连看JAVA源代码是什么? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class lianli ...

  3. js小游戏动物连连看代码

    下载地址 js小游戏动物连连看代码,有多种语言切换,默认是中文.不用部署本地解压即可预览. dd:

  4. 连连看c语言源程序,连连看源代码C语言

    连连看源代码C语言Tag内容描述: 1.连连看游戏C语言源代码 include stdio h include graphics h include stdlib h include math h i ...

  5. 连连看+php,java基于swing实现的连连看代码

    本文实例讲述了java基于swing实现连连看代码.分享给大家供大家参考. 主要功能代码如下:package llkan; import javax.swing.*; import java.awt. ...

  6. DL之RNN:人工智能为你写代码——基于TF利用RNN算法实现生成编程语言代码(C++语言)、训练测试过程全记录

    DL之RNN:基于TF利用RNN算法实现生成编程语言代码(C语言).训练&测试过程全记录 目录 输出结果 监控模型 训练&测试过程全记录 训练的数据集展示 输出结果 1.test01 ...

  7. c语言成绩管理程序设计,成绩管理程序设计报告(含代码C语言

    <成绩管理程序设计报告(含代码C语言>由会员分享,可在线阅读,更多相关<成绩管理程序设计报告(含代码C语言(19页珍藏版)>请在人人文库网上搜索. 1.大连民族学院计算机科学与 ...

  8. server酱php推送代码,多种语言调用Server酱推送微信模板消息

    把Server酱用来推送报警信息或日志是非常方便的,接入成本非常低也很简单 最近使用了一个 Python 的自动签到脚本,进行自动签到,但是每天签到完白天还需要看日志或者访问网站查看是否成功 这还不如 ...

  9. Arduino RGB颜色渐变代码(附上C语言版本)

    这段代码来自 Smooth RGB LED Transitions with Johnny-Five - Arduino Project Hub const int redPin = 11; cons ...

最新文章

  1. 如何使用 Python 构建推荐引擎?
  2. 收藏 | 75道常见AI面试题助你清扫知识盲点(附解析)
  3. C++ OOP学习记录
  4. 【PP生产订单】入门介绍(四)
  5. c语言线程面试题,java多线程面试题 PDF 下载
  6. 攻防世界-web-ics-04-从0到1的解题历程writeup
  7. 向silverlight传递自定义参数
  8. jBPM4.4 window下启动tomcat
  9. 北京市中 高英语听说计算机考,2021年北京高考首次英语听说机考时间确定,共五种题型...
  10. SAP License:SAP S/4HANA Cloud [ERP 云]
  11. PHP-----文件系统的交互
  12. html留言页面设计,html的留言板制作(js)
  13. mysql 分析函数 平均_mysql平均函数
  14. 更新三转的skills.txt
  15. SL-积雪效果(hitTest)雪人(snowman)
  16. Flutter 中TextField的hintText不居中与光标位置不一致
  17. php上传公众号临时素材-微信开发素材管理6
  18. computer-06 其它
  19. WampServer修改MySQL密码
  20. 关于卡尔曼及卡尔曼增益的理解【精】

热门文章

  1. Maven的Archetype简介
  2. 奇异值分解(SVD)相关知识
  3. 机器学习中的不平衡分类方法(part5)--决策树与随机森林
  4. Tableau研学小课堂(part7)--计算字段
  5. Hive入门之数据类型
  6. 会导致所有者权益减少的项目是_处置固定资产而发生的净损失为什么会导致所有者权益减少...
  7. SAP 电商云 Spartacus UI 的 style library 介绍
  8. SAP Spartacus 会使用 Session timeout 吗?
  9. SAP Spartacus翻译 i18n - internationalization 的工作原理
  10. Angular单元测试框架beforeEach和it的执行顺序