使用示例:
>> fx = [1 2 3;4 5 6;7 8 9]
fx =
1 2 3
4 5 6
7 8 9
>> x = [2;2;3]
x =
2
2
3
>> y = [2;3;1]
y =
2
3
1
vfx = interp2(fx,x,y,'linear')
vfx =
5
8
3
interp2是针对矩阵的,其中fx应该是个矩阵,错误提示是说你的fx至少应该是一个22的矩阵;另外, vfx = interp2(fx,x,y,'linear')这句命令是在求矩阵fx中(x,y)点的值,x,y可以是一个点,也可以是一系列点,如列子所示。
/
snake
/
#include <stdioh>
#include <stdlibh>
#include <stringh>
#define DEBUG 0
#define printpos() \
printf("File: %s\tLine: %d\n", __FILE__, __LINE__); fflush(stdout);
#define CALLOC(ARRAY, NUM, TYPE) \
ARRAY = (TYPE) calloc(NUM, sizeof(TYPE)); \
if (ARRAY == NULL) { \
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed\n"); \
exit(0); \
}
#define REALLOC(ARRAY, NUM, TYPE) \
ARRAY = (TYPE) realloc(ARRAY, (NUM)sizeof(TYPE)); \
if (ARRAY == NULL) { \
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed\n"); \
exit(0); \
}
const int START = -1;
const int HOME = -2;
#if DEBUG
int m=4, n=4;
int a[4][4] = {{7, 0, 4, 18}, {4, 0, 1, 1}, {15, 7, 11, -1}, {0, 12, -2, 0}};
#else
int m=0, n=0;
int a=NULL;
#endif
struct pos {
int x;
int y;
};
typedef struct pos pos;
struct node {
pos p;
int mv;
int n;
};
typedef struct node node;
const pos mv[4] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
/
get m, n, a and check them
/
int setup()
{
int nstart=0, nhome=0;
int i, j;
#if DEBUG
#else
//get the dimension of the matrix and allocate memory
printf("Please input the number of rows of the matrix: ");
scanf("%d", &m);
if (m<=0) {
printf("Number of rows must be greater than 0\n");
exit(0);
}
a = (int) calloc(m, sizeof(int));
if (a == NULL) {
printf("Allocate memory failed\n");
exit(1);
}
printf("Please input the number of columns of the matrix: ");
scanf("%d", &n);
if (n<=0) {
printf("Number of columns must be greater than 0\n");
exit(0);
}
for (i=0; i<m; i++) {
a[i] = (int) calloc(n, sizeof(int));
if (a[i] == NULL) {
printf("Allocate memory failed\n");
exit(1);
}
}
//get the matrix
printf("Please input the matrix, entities seperated by blank:\n");
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
scanf("%d", &a[i][j]);
}
}
#endif
//check the matrix
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
if (a[i][j] == START) {
nstart++;
if (nstart > 1) {
printf("More than 1 starting point\n");
exit(0);
}
} else if (a[i][j] == HOME) {
nhome++;
if (nhome > 1) {
printf("More than 1 home point\n");
exit(0);
}
} else if (a[i][j] < 0) {
printf("a[%d][%d] = %d has no meaning\n", i, j, a[i][j]);
exit(0);
}
}
}
if (nstart == 0) {
printf("No starting point\n");
exit(0);
}
if (nhome == 0) {
printf("No home point\n");
exit(0);
}
//output the matrix
printf("The matrix (%d X %d):\n", m, n);
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
return 0;
}
int solve(node optpath)
{
pos dest; //destinating point
node curpath = NULL; //current path
node sol = NULL;
int nsol = 0;
int nsteps; //number of steps
int i, j;
int curmv = -1;
int sucmv = 0; //sucessfully moved
int sum;
int maxsum=0;
//setup starting point
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
if (a[i][j] == START) {
destx = i;
desty = j;
break;
}
}
}
nsteps = 0;
CALLOC(curpath, nsteps+1, node);
curpath[0]px = destx;
curpath[0]py = desty;
curpath[0]mv = -1;
a[destx][desty] = 0;
curmv = 0;
while (1) {
for (sucmv=0, curmv=curpath[nsteps]mv+1; curmv<4; curmv++) {
destx = curpath[nsteps]px + mv[curmv]x;
desty = curpath[nsteps]py + mv[curmv]y;
if (destx < 0 || destx >= m || desty < 0 || desty >= n) {
curpath[nsteps]mv = curmv;
continue;
}
if (a[destx][desty] == 0) {
curpath[nsteps]mv = curmv;
continue;
}
nsteps++;
REALLOC(curpath, nsteps+1, node);
curpath[nsteps]px = destx;
curpath[nsteps]py = desty;
curpath[nsteps-1]mv = curmv;
curpath[nsteps]mv = -1;
curpath[nsteps]n = a[destx][desty];
a[destx][desty] = 0;
sucmv = 1;
break;
}
if (sucmv) {
if (curpath[nsteps]n == HOME) {
nsol++;
REALLOC(sol, nsol, node);
CALLOC(sol[nsol-1], nsteps+1, node);
memcpy(sol[nsol-1], curpath, (nsteps+1)sizeof(node));
//back
a[curpath[nsteps]px][curpath[nsteps]py] = curpath[nsteps]n;
nsteps--;
if (nsteps == -1 && curpath[0]mv == 3) break;
REALLOC(curpath, nsteps+1, node);
} else {
continue;
}
} else {
a[curpath[nsteps]px][curpath[nsteps]py] = curpath[nsteps]n;
nsteps--;
if (nsteps == -1 && curpath[0]mv == 3) break;
REALLOC(curpath, nsteps+1, node);
}
}
//printf("number of solutions: %d\n", nsol);
for (maxsum=0, i=0; i<nsol; i++) {
//printf("Solution %d \n", i);
//printf("\tPath: ");
sum = -1HOME;
for (j=0; ; j++) {
//printf("(%d, %d)\t", sol[i][j]px, sol[i][j]py);
sum += sol[i][j]n;
if (sol[i][j]mv == -1) break;
}
//printf("\n\tSum of apples: %d\n", sum);
if (sum>maxsum) {
maxsum = sum;
optpath = sol[i];
}
}
return 0;
}
int output(node path)
{
int i=0, sum=0;
printf("Path: ");
sum = -1HOME;
for (i=0; ; i++) {
printf("(%d, %d)\t", path[i]px, path[i]py);
sum += path[i]n;
if (path[i]mv == -1) break;
}
printf("\nSum of apples: %d\n", sum);
return 0;
}
int main()
{
node path=NULL;
setup();
solve(&path);
output(path);
return 0;
}
编译、链接、运行程序,输入与输出如下:
:!gcc -Wall tmpc -o tmp
:! /tmp
Please input the number of rows of the matrix: 5
Please input the number of columns of the matrix: 5
Please input the matrix, entities seperated by blank:
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0 9
1 2 5 1 0 7
The matrix (5 X 5):
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0
9 1 2 5 1
Path: (2, 4) (1, 4) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 3) (3, 3) (4, 3) (4, 2) (4, 1) (4, 0) (3, 0) (3, 1) (2, 1) (1, 1) (0, 1) (0, 0) (1, 0)
Sum of apples: 108
:!gcc -Wall tmpc -o tmp
:! /tmp
Please input the number of rows of the matrix: 4
Please input the number of columns of the matrix: 4
Please input the matrix, entities seperated by blank:
7 0 4 18
4 0 1 1
15 7 11 -1
0 12 -2 0
The matrix (4 X 4):
7 0 4 18
4 0 1 1
15 7 11 -1
0 12 -2 0
Path: (2, 3) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 1) (3, 1) (3, 2)
Sum of apples: 54
/因为当时只考虑到了代码可通过,没有考虑代码的质量,所以重复利用率差,不要随意改动哦
运行是完全通过的但是会一闪一闪的/
#include<iostream>
#include<conioh>
#include<timeh>
#include<windowsh>
#define X 15
#define Y 15
int score=0;
char ch[X][Y];
char lch = 's';
char nch = 's';
class Snake
{
public:
Snake() { nx = 8;ny = 8;lx = 9;ly = 9;next = NULL; }
Snake(Snake&r);//建立蛇身
void input();//把数据输入到图像
void control();//控制贪吃蛇
void show();//显示图像
void check();//检测食物是否被吃掉
bool gameover();//检测游戏是否结束
int nx;
int ny;
int lx;
int ly;
Snakenext;
};
Snake head;
namespace Food
{
int x;
int y;
void NEW()
{
Snakep=head;
x = rand() % X;
y = rand() % Y;
while(p!=NULL)
{
if(p->nx==x&&p->ny==y)
{
x = rand() % X;
y = rand() % Y;
p = head;
}
else
p=p->next;
}
ch[x][y]='$';
}
};
Snake::Snake(Snake&r)
{
this->nx = rlx;
this->ny = rly;
rnext = this;
this->next = NULL;
}
void Snake::show()//显示图像
{
system("cls");
//std::cout << " ------------------------------ \n";
for (int y = 0; y<Y; ++y)
{
//std::cout << "|";
for (int x = 0; x < X; ++x)
std::cerr << ch[x][y] << " " ;
std::cerr <<"|"<< std::endl;
}
std::cout << " ------------------------------ \n";
printf("x = %d,y = %d\n",nx,ny);
printf("x = %0X,y = %0X\n",&nx,&ny);
printf("score = %d\n",score);
}
void Snake::control()//键盘控制
{
if (_kbhit() == 1)
{
nch = _getch();
lx = nx;
ly = ny;
if (nch == 'w' || nch == 's' || nch == 'a' || nch == 'd')
{
lch = nch;
switch (nch)
{
case 'w':if (ny == 0) ny = Y - 1; else --ny; break;
case 's':if (ny == Y - 1) ny = 0; else ++ny; break;
case 'a':if (nx == 0) nx = X - 1; else --nx; break;
case 'd':if (nx == X - 1) nx = 0; else ++nx; break;
}
}
else
{
switch (lch)
{
case 'w':if (ny == 0) ny = Y - 1; else --ny; break;
case 's':if (ny == Y - 1) ny = 0; else ++ny; break;
case 'a':if (nx == 0) nx = X - 1; else --nx; break;
case 'd':if (nx == X - 1) nx = 0; else ++nx; break;
}
}
}
else
{
lx = nx;
ly = ny;
if (nch == 'w' || nch == 's' || nch == 'a' || nch == 'd')
{
lch = nch;
switch (nch)
{
case 'w':if (ny == 0) ny = Y - 1; else --ny; break;
case 's':if (ny == Y - 1) ny = 0; else ++ny; break;
case 'a':if (nx == 0) nx = X - 1; else --nx; break;
case 'd':if (nx == X - 1) nx = 0; else ++nx; break;
}
}
else
{
switch (lch)
{
case 'w':if (ny == 0) ny = Y - 1; else --ny; break;
case 's':if (ny == Y - 1) ny = 0; else ++ny; break;
case 'a':if (nx == 0) nx = X - 1; else --nx; break;
case 'd':if (nx == X - 1) nx = 0; else ++nx; break;
}
}
}
}
void Snake::input()//把对象输入到图像
{
Snakep = head;
ch[p->lx][p->ly] = ' ';
ch[p->nx][p->ny] = '';
while (p->next != NULL)
{
p->next->lx = p->next->nx;
p->next->ly = p->next->ny;
p->next->nx = p->lx;
p->next->ny = p->ly;
p = p->next;
ch[p->lx][p->ly] = ' ';
ch[p->nx][p->ny] = '';
}
}
void Snake::check()//检测食物是否被吃掉
{
Snakep=head;
if (p->nx == Food::x&&p->ny == Food::y)
{
Snakep = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = new Snake(p);
Food::NEW();
++score;
}
}
bool Snake::gameover()//检测游戏是否结束
{
Snakep = head;
Snakepn = head->next;
while (p != NULL)
{
while (pn != NULL)
{
if (p->nx == pn->nx&&p->ny == pn->ny)
{
std::cout << "Game Over\n";
return true;
}
pn = pn->next;
}
p = p->next;
}
return 0;
}
int main()
{
//初始化
srand(time(NULL));
for (int y = 0; y<Y; ++y)
for (int x = 0; x<X; ++x)
ch[x][y] = ' ';
head = new Snake;
Food::NEW();
std::cout<<"游戏规则:\n1wsad(小写)控制蛇\n2若蛇节数超过2,那么无法反方向运动,\n若反方向运动则GameOver\n3''为蛇,'$'为食物\n";
system("pause");
//开始游戏
while (1)
{
head->control();
head->input();
head->check();
if(head->gameover())
break;
head->show();
Sleep(100);
}
Sleep(1000);
system("pause");
return 0;
}
/------------------------------------------------------------------------------------
当然你也可以用下面这个,经过一些奇怪的算法,当然可读性极差,不闪,但是有时有些莫名其妙的bug的,这是把输出图像变成了一个一维数组,用一维数组来模拟直角坐标系
------------------------------------------------------------------------------------/
#include<iostream>
#include<windowsh>
#include<conioh>
#include<timeh>
#define X 32
#define Y 17
char ch[544];
char nch = 's';
char lch = 's';
int score = 0;
class Snake
{
public:
Snake();//创建蛇头
void Add();//添加蛇身
void Show();//显示图像
bool GameOver();//检测游戏是否结束
void Input();//输入数据
void Check();//检测食物是否被吃
void Control();//输入控制
void INIT();
int GetX(){return nx;}
int GetY(){return ny;}
Snake next;
private:
int lx;
int ly;
int nx;
int ny;
};
Snakehead;
Snakefinally;
namespace Food
{
int x;
int y;
void GetXY()
{
std::cout<<"Food x = "<<x<<"Food y = "<<y<<"\n";
}
void NEW()
{
Snakep=head;
x = (rand()%15)+1;
y = (rand()%15)+1;
while(p!=NULL)
{
if(p->GetX()==x&&p->GetY()==y)
{
x = (rand()%15)+1;
y = (rand()%15)+1;
p=head;
}
p=p->next;
}
ch[y32+x2-1]='$';
}
};
void Snake :: INIT()//初始化
{
for(int n=0;n<=543;++n)
ch[n]=' ';
for(int n=1;n<30;++n)
ch[n]='-';
for(int n=1;n<30;++n)
ch[n+512]='-';
for(int n=0;n<=16;++n)
ch[n32+31]='\n';
for(int n=1;n<=16;++n)
ch[n32+30]='|';
for(int n=1;n<=16;++n)
ch[n32]='|';
ch[543]='\0';
next=NULL;
Food::NEW();
lx = 4;
ly = 4;
nx = 4;
ny = 5;
ch[ly32+lx2-1]='';
}
Snake :: Snake()//创建蛇头head
{
}
void Snake :: Add()//添加蛇身
{
finally->next = new Snake;
finally->next->nx=finally->lx;
finally->next->ny=finally->ly;
finally->next->next=NULL;
finally=finally->next;
}
void Snake :: Control()//控制输入
{
if(kbhit()==1){
nch=getch();
if(nch=='w'||nch=='s'||nch=='a'||nch=='d'){
lch=nch;
switch(nch)
{
case 'w':if(ny==1) ny=15;else ny--;break;
case 's':if(ny==15) ny=1;else ny++;break;
case 'a':if(nx==1) nx=15;else nx--;break;
case 'd':if(nx==15) nx=1;else nx++;break;
}
}else{
switch(lch)
{
case 'w':if(ny==1) ny=15;else ny--;break;
case 's':if(ny==15) ny=1;else ny++;break;
case 'a':if(nx==1) nx=15;else nx--;break;
case 'd':if(nx==15) nx=1;else nx++;break;
}
}
}else{
switch(lch)
{
case 'w':if(ny==1) ny=15;else ny--;break;
case 's':if(ny==15) ny=1;else ny++;break;
case 'a':if(nx==1) nx=15;else nx--;break;
case 'd':if(nx==15) nx=1;else nx++;break;
}
}
}
void Snake :: Show()//显示图像
{
std::cerr<<ch<<"\nscore = "<<score<<"\t";
}
void Snake :: Input()//输入数据
{
Snakep = head;
ch[(p->lx)2+(p->ly)32-1] = ' ';
ch[(p->nx)2+(p->ny)32-1] = '';
while (p->next != NULL)
{
p->next->lx = p->next->nx;
p->next->ly = p->next->ny;
p->next->nx = p->lx;
p->next->ny = p->ly;
p = p->next;
ch[(p->lx)2+(p->ly)32-1] = ' ';
ch[(p->nx)2+(p->ny)32-1] = '';
}
head->lx=head->nx;
head->ly=head->ny;
}
bool Snake :: GameOver()//检测游戏是否结束
{
Snake p = head;
Snake pl ;
while(p!=NULL)
{
pl=p->next;
while(pl!=NULL)
{
if(p->nx==pl->nx&&p->ny==pl->ny){
std::cout<<"\nGameOver\n";
return true;
}
pl=pl->next;
}
p=p->next;
}
return false;
}
void Snake :: Check()//检测食物是否被吃掉
{
if(head->nx==Food::x&&head->ny==Food::y)
{
Food::NEW();
head->Add();
score++;
}
}
int main(int argc,char argv[])
{
srand(time(NULL));
head=new Snake;
head->INIT();
finally = head;
while(1)
{
head->Control();//控制输出
head->Input();//输入数据
system("cls");
head->Show();//显示图像
Sleep(150);
if(head->GameOver())//检测游戏是否结束
break;
head->Check();//检测食物是否被吃
}
Sleep(1000);
printf("请按任意键退出\n");
_getch();
return 0;
}
#include<bits/stdc++h>
#include<stdlibh>
#include<dosh>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /食物的结构体/
{
int x; /食物的横坐标/
int y; /食物的纵坐标/
int yes; /食物是否出现的变量/
}food;
struct Snack /蛇的结构体/
{
int x[N];
int y[N];
int node; /蛇的节数/
int direction; /蛇的方向/
int life; /蛇的生命,0活着,1死亡/
}snake;
void Init(void); /图形驱动/
void Close(void); /关闭游戏函数/
void DrawK(void); /画图函数/
void GameOver(void);/输出失败函数/
void GamePlay(); /游戏控制函数 主要程序/
void PrScore(void); /分数输出函数/
DELAY(char ch)/调节游戏速度/
{
if(ch=='3')
{
delay(gamespeed); /delay是延迟函数/
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/游戏开始菜单/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/主函数/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /画出上边框/
rectangle(i,451,i+10,460); /画出下边框/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /画出左边框/
rectangle(601,i,610,i+10); /画出右边框/
}
}
void GamePlay(char ch)
{
randomize(); /随机数发生器/
foodyes=1; /1代表要出现食物,0表示以存在食物/
snakelife=0;
snakedirection=1;
snakex[0]=100;snakey[0]=100;
snakex[1]=110;snakey[1]=100;
snakenode=2;
PrScore();
while(1) /可以重复游戏/
{
while(!kbhit()) /在没有按键的情况下蛇自己移动/
{
if(foodyes==1) /需要食物/
{
foodx=rand()%400+60;
foody=rand()%350+60; /使用rand函数随机产生食物坐标/
while(foodx%10!=0)
foodx++;
while(foody%10!=0)
foody++; /判断食物是否出现在整格里/
foodyes=0; /现在有食物了/
}
if(foodyes==0) /有食物了就要显示出来/
{
setcolor(GREEN);
rectangle(foodx,foody,foodx+10,foody-10);
}
for(i=snakenode-1;i>0;i--) /贪吃蛇的移动算法/
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1]; /贪吃蛇的身体移动算法/
}
switch(snakedirection) /贪吃蛇的头部移动算法,以此来控制移动/
{
case 1:snakex[0]+=10;break;
case 2:snakex[0]-=10;break;
case 3:snakey[0]-=10;break;
case 4:snakey[0]+=10;break;
}
for(i=3;i<snakenode;i++) /判断是否头部与身体相撞/
{
if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])
{
GameOver();
snakelife=1;
break;
}
}
/下面是判断是否撞到墙壁/
if(snakex[0]<55||snakex[0]>595||snakey[0]<55||snakey[0]>455)
{
GameOver();
snakelife=1;
}
if(snakelife==1) /如果死亡就退出循环/
break;
if(snakex[0]==foodx&&snakey[0]==foody) /判断蛇是否吃到食物/
{
setcolor(0);
rectangle(foodx,foody,foodx+10,foody-10); /吃的食物后用黑色将食物擦去/
snakex[snakenode]=-20;snakey[snakenode]=-20; /现把增加的一节放到看不到的地方去/
snakenode++;
foodyes=1;
score+=10;
PrScore();
}
setcolor(4); /每次移动后将后面的身体擦去/
for(i=0;i<snakenode;i++)
rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snakex[snakenode-1],snakey[snakenode-1],snakex[snakenode-1]+10,snakey[snakenode-1]-10);
}
if(snakelife==1)
break;
key=bioskey(0); /接受按键/
if(key==ESC)
break;
else
if(key==UP&&snakedirection!=4)/判断是否改变方向/
snakedirection=3;
else
if(key==RIGHT&&snakedirection!=2)
snakedirection=1;
else
if(key==LEFT&&snakedirection!=1)
snakedirection=2;
else
if(key==DOWN&&snakedirection!=3)
snakedirection=4;
}
}
void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
以上就是关于matlab中snake算法的snakedeform函数的参数设置全部的内容,包括:matlab中snake算法的snakedeform函数的参数设置、求助,用C语言并用回溯算法编写的有关蛇吃苹果走迷宫的程序,题目如下:、求一个C++的贪吃蛇游戏源代码,要标注解释的。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)