12.14算法作业

1、时间复杂度分析
代码:
#include <stdio.h>
#include <stdlib.h>

struct NODE{
int x;
int y;
NODE* pNext;
};

int map[5][5] = {
{1, 0, 1, 0, 0},
{1, 0, 1, 1, 1},
{1, 0, 1, 0, 1},
{1, 0, 1, 0, 1},
{1, 1, 1, 0, 1},
};

int step[5][5] = {
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};

int size = 0;
NODE* head = NULL;
NODE* tail = NULL;
NODE* tempNode = NULL;

void push(NODE* node){
if (tail == NULL){
head = node;
tail = node;
}else{
tail->pNext = node;
tail = node;
}
size++;
}

NODE* pop(){
tempNode = head;
head = head->pNext;

if (head == NULL){
	tail = NULL;
}

size--;

return tempNode;

}

void bsf(){
while(size > 0){
NODE* node = pop();

	//bottom
	if (node->x + 1 <= 4 && step[node->x + 1][node->y] == 0 && map[node->x + 1][node->y] == 1){
		NODE* nextNode = (NODE*)malloc(sizeof(NODE));
		nextNode->x = node->x + 1;
		nextNode->y = node->y;
		nextNode->pNext = NULL;
		push(nextNode);			
		 
		step[nextNode->x][nextNode->y] = step[node->x][node->y] + 1;
	} 
	
	//right
	if (node->y + 1 <= 4 && step[node->x][node->y + 1] == 0 && map[node->x][node->y + 1] == 1){
		NODE* nextNode = (NODE*)malloc(sizeof(NODE));
		nextNode->x = node->x;
		nextNode->y = node->y + 1;
		nextNode->pNext = NULL;
		push(nextNode);		
		 
		step[nextNode->x][nextNode->y] = step[node->x][node->y] + 1;
	}
	//top
	if (node->x - 1 >= 0 && step[node->x - 1][node->y] == 0 && map[node->x - 1][node->y] == 1){
		NODE* nextNode = (NODE*)malloc(sizeof(NODE));
		nextNode->x = node->x - 1;
		nextNode->y = node->y;
		nextNode->pNext = NULL;
		push(nextNode);					
		step[nextNode->x][nextNode->y] = step[node->x][node->y] + 1;						
	}
	
	//left
	if (node->y - 1 >= 0 && step[node->x][node->y - 1] == 0 && map[node->x][node->y - 1] == 1){
		NODE* nextNode = (NODE*)malloc(sizeof(NODE));
		nextNode->x = node->x;
		nextNode->y = node->y - 1;
		nextNode->pNext = NULL;
		push(nextNode);					
		step[nextNode->x][nextNode->y] = step[node->x][node->y] + 1;						
	}
	


}

}

int dsf(int cx, int cy){
if (cx == 4 && cy == 4){
printf("[%d, %d] ", cx, cy);
return 1;
}

//bottom
if (cx + 1 <= 4 && step[cx + 1][cy] == step[cx][cy] + 1){
	if (dsf(cx+1, cy) == 1){
		printf("[%d, %d] ", cx, cy);
		return 1;
	}
} 

//right
if (cy + 1 <= 4 && step[cx][cy + 1] == step[cx][cy] + 1){
	if (dsf(cx, cy + 1) == 1){
		printf("[%d, %d] ", cx, cy);
		return 1;
	}
} 

//top
if (cx - 1 >= 0 && step[cx - 1][cy] == step[cx][cy] + 1){
	if (dsf(cx - 1, cy) == 1){
		printf("[%d, %d] ", cx, cy);
		return 1;
	}
} 

//left 
if (cy - 1 >= 0 && step[cx][cy - 1] == step[cx][cy] + 1){
	if (dsf(cx, cy - 1) == 1){
		printf("[%d, %d] ", cx, cy);
		return 1;
	}
} 

return 0;

}

int main(){
NODE* node = (NODE*)malloc(sizeof(NODE));
node->x = 0;
node->y = 0;
node->pNext = NULL;
step[0][0] = 1;
push(node);

bsf();

for(int i = 0; i < 5; i++){
	for(int j = 0; j < 5; j++){
		printf("%d\t", step[i][j]);
	}
	printf("\n");
}//O(N2) 

printf("逆序路径为:\n");
//打印出路径 
if (dsf(0, 0) == 1){
	printf("yes");
}
else{
	printf("no");
}

}

分析:递归部分的时间复杂度为O(n),但主函数中有双重循环,时间复杂度为O(n2),所以这段代码时间复杂度为O(n2)

2、背包问题
代码:
#include<stdio.h>
int max(int a,int b){
return a>b?a:b;
}
struct Thing{
int w;//体积
int v;//价值
}list[101];

int dp[101][101]={0};//初始化二维数组

int main(){
int s,n;//背包容量和物品总数
scanf("%d",&s);
scanf("%d",&n);

		for(int i=1;i<=n;i++){
			scanf("%d",&list[i].w);
			scanf("%d",&list[i].v);
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=s;j>=list[i].w;j--){
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-list[i].w]+list[i].v);
			}
			for(int j=list[i].w-1;j>=0;j--){
				dp[i][j]=dp[i][j-1];
			}
		}
	
 
printf("%d\n",dp[n][s]);

}
运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值