Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX_ROW 9 //hang
#define MAX_COL 9 //cot
#define MAX 100

typedef struct POINT{


int row;
int column;
int value;
bool visited;
struct POINT *prev;
}point_t;
// tao cau truc hang doi
typedef struct{
point_t data[MAX];
int front;
int rear;
}Queue;
void initQueue(Queue*q);
int isEmpty(Queue q);
int isFull(Queue q);
void enQueue(Queue *q,point_t x);
point_t deQueue(Queue *q);
bool checkCoordinate(int dong,int cot);
void findAroundPoint(int dong, int cot, point_t surroundingPnt[4],
int*count,point_t matrix[MAX_ROW][MAX_COL]);
void findShortestPath(int dong,int cot);
// ham chinh
point_t matrix[MAX_ROW][MAX_COL];
int main(){
int i,j;
int dong, cot;
// hardcode
int matran[MAX_ROW][MAX_COL]={
{1,0,0,0,1,0,1,1,0}, //0
{1,1,0,1,1,1,0,0,1}, //1
{0,1,0,1,1,0,1,0,1},//2
{0,1,1,0,0,1,0,1,1},//3
{0,0,1,0,1,0,1,0,0},//4
{1,1,1,0,0,0,0,1,1},//5
{1,0,1,1,1,1,1,0,0},//6
{1,1,1,0,0,0,1,0,1},//7
{0,0,0,1,1,1,1,1,0}};//8

// in ma tran ra man hinh


printf("CHUONG TRINH TIM DUONG DI NGAN NHAT TU 0(0,0) DEN A(dong,cot):\n\n");
printf("Ma tran 9x9 bieu dien cho mat phan 2 chieu:\n");
for( i = 0;i<MAX_ROW;i++){
for( j = 0; j< MAX_COL;j++){
matrix[i][j].row = i;
matrix[i][j].column = j;
matrix[i][j].value = matran[i][j];
matrix[i][j].visited = false;
matrix[i][j].prev = NULL;
printf("%d ",matrix[i][j].value);
}
printf("\n");
}
printf("Toa do cua o xuat phat la O(0,0).\n");
do {
printf("Hay nhap toa do cua o dich den A:\n");
printf("Nhap dong: ");
scanf("%d", &dong);
printf("Nhap cot: ");
scanf("%d", &cot);

if (!checkCoordinate(dong, cot))
{
printf("Gia tri cua Ax phai tu 0 den 8!\n");
printf("Gia tri cua Ay phai tu 0 den 8!\n\n");
}
}while (!checkCoordinate(dong, cot));
//
if(checkCoordinate(dong,cot)){
printf("Toa do A(%d,%d)\n",dong,cot);
}
//
point_t surroundingPnt[4];
int count;
findAroundPoint(dong, cot, surroundingPnt, &count, matrix);
// ket qua
printf("So diem xung quanh diem o hang %d, cot %d : %d\n",dong,cot,count);
//
findShortestPath(dong,cot);

return 0;
}
// khoi tao hang doi
void initQueue(Queue*q){

q->front = 0;
q->rear = -1;

}
// tao ham de xem hang doi co rong khong
int isEmpty(Queue q){

return (q.rear<q.front);

}
// tao ham xem hang doi da day hay chua
int isFull(Queue q){

if((q.rear - q.front) == MAX - 1){


return 1;
}
else{
return 0;
}

}
// them phan tu vao vi tri cuoi
void enQueue(Queue *q,point_t x){
int i ;
if(!isFull(*q)){
if(q->rear == MAX-1){
for( i = q->front; i <= q->rear; i++){
q->data[i - q->front] = q->data[i];
}
// vi tri cuoi bang MAX - vi tri dau - 1
q->rear = MAX - q->front - 1;
q->front = 0; // gan vi tri dau ve khong

}
// them vi tri cuoi len mot
q->rear = q->rear+1;
q->data[q->rear]=x; // gia tri cua vi tri cuoi bang mang gia tri cua mang
hai chieu

}
}
// lay phan tu dau tien ra khoi hang doi
point_t deQueue(Queue *q) {
point_t d;

if (!isEmpty(*q)) {
d = q->data[q->front];
q->front = q->front +1;
}

if (q->front > q->rear) {


initQueue(q);
}
return d;
}
// ham kiem tra toa do nhap vao co nam trong so dong so cot quy dinh khong
bool checkCoordinate(int dong,int cot){
bool ret = false;
if(dong >= 0&& cot >= 0){
if(dong<MAX_ROW && cot<MAX_COL){
ret = true;
}
}
return ret;
}
//Cau 3
//Tim cac diem xung quanh xet xem co diem nao bang 1 khong
void findAroundPoint(int dong, int cot, point_t surroundingPnt[4],
int*count,point_t matrix[MAX_ROW][MAX_COL]){
int tempCnt = 0;
int i = 0;
for( i = 0;i<4;i++){
surroundingPnt[i].value = 0;
}

// kiem tra vi tri ben phai


if(checkCoordinate(dong,cot+1)==true){
if( matrix[dong][cot+1].value == 1){
surroundingPnt[tempCnt].value = matrix[dong][cot+1].value;
++tempCnt;
}
}
// kiem tra vi tri ben trai
if(checkCoordinate(dong,cot-1)==true){
if( matrix[dong][cot-1].value == 1){
surroundingPnt[tempCnt].value = matrix[dong][cot-1].value;
++tempCnt;
}
}
// kiem tra vi tri ben tren
if(checkCoordinate(dong-1,cot)==true){
if( matrix[dong - 1][cot].value == 1){
surroundingPnt[tempCnt].value = matrix[dong-1][cot].value;
++tempCnt;
}
}
// kiem tra vi tri ben duoi
if(checkCoordinate(dong+1,cot) == true){
if( matrix[dong+1][cot].value == 1){
surroundingPnt[tempCnt].value = matrix[dong+1][cot].value;
++tempCnt;
}
}
*count = tempCnt;
}

// Ham in ra duong di ngan nhat


void printPath(int x,int y)
{ int i;
int count = 0 , max_count;
point_t *destination = &matrix[x][y]; // Con tro tro den ma tran
int arrRow[100];
int arrColumn[100];
while (destination != NULL)
{
arrRow[count] = destination->row;
arrColumn[count] = destination->column;
destination = destination->prev;
count++;
}
printf("\nDuong di ngan nhat tu O(0,0) den A(%d,%d) co do dai la %d
o: \n", x, y, count);

for ( i=count-1 ; i>=0 ; i--)


{
printf("(%d,%d)", arrRow[i], arrColumn[i]);
if(i>0)
printf("->");
}
}
void findShortestPath(int dong,int cot){
point_t p ; // bien luu gia tri di duoc
point_t pp[4]={0}; //bien luu cac gia tri xung quanh
int i,j;
int count = 0;
Queue queue;
initQueue(&queue);
matrix[0][0].visited = true;
enQueue(&queue,matrix[0][0]);
bool found = false;
while(isEmpty(queue)== false && found == false){
p = deQueue(&queue);
findAroundPoint(p.row,p.column,pp,&count,matrix);
for(i = 0;i<count;i++){
if(pp[i].visited == false){
matrix[pp[i].row][pp[i].column].visited = true;
matrix[pp[i].row][pp[i].column].prev = &matrix[p.row][p.column];
printf("(%d,%d) ",pp[i].row,pp[i].column);
if(pp[i].row == dong && pp[i].column == cot){
found = true;
break;
}
else{
enQueue(&queue,pp[i]);
}
}
}
// if(found == true){
// point_t *m = &matrix[dong][cot];
// int n = 0;
// point_t arr[100];
// while(m!=NULL){
// arr[n] = *m;
// m = m->prev;
// n++;
// }
// printf("\nDuong di ngan nhat tu O(0, 0) den A(%d, %d) co do dai la %d o\
n",dong, cot, n);
// i=0;
// for(i = n-1;i >= 0 ;i--){
// printf("(%d, %d)",arr[i].row,arr[i].column);
// if(i != 0){
// printf("->");
// }
// }
// }else{
// printf("\nKhong co duong di tu O(0, 0) den A(%d, %d)", dong,cot);
// }
if(found ==
true) // Neu tim duoc
diem dich
{
printPath(dong, cot ); // In ra toa do cac
diem tren duong di
}
else{ // Neu
khong tim duoc diem dich
printf("Khong tim duoc duong di tu o xuat phat O den o dich den A.");
// In ra "Khong tim duoc"
}

}
}

You might also like