十字交叉双向循环链表如何解决这个作为匹配的算法,怎么C语言实现的呢?

Problem Description
A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem instead of going on with informal arguments to decide which tickets to buy.

The movie theater has R rows of C seats each, and they can see a map with the currently available seats marked. They decided that seating close to each other is all that matters, even if that means seating in the front row where the screen is so big it’s impossible to see it all at once. In order to have a formal criteria, they thought they would buy seats in order to minimize the extension of their group.

The extension is defined as the area of the smallest rectangle with sides parallel to the seats that contains all bought seats. The area of a rectangle is the number of seats contained in it.

They’ve taken out a laptop and pointed at you to help them find those desired seats.

Input
Each test case will consist on several lines. The first line will contain three positive integers R, C and K as explained above (1 <= R,C <= 300, 1 <= K <= R × C). The next R lines will contain exactly C characters each. The j-th character of the i-th line will be ‘X’ if the j-th seat on the i-th row is taken or ‘.’ if it is available. There will always be at least K available seats in total.
Input is terminated with R = C = K = 0.

Output
For each test case, output a single line containing the minimum extension the group can have.

Sample Input
3 5 5
...XX
.X.XX
XX...
5 6 6
..X.X.
.XXX..
.XX.X.
.XXX.X
.XX.XX
0 0 0

Sample Output
6
9

2个回答

#include
#include

typedef struct Node{
int data;
struct Node *front,*next; //双向链表每个节点都有两个指针
}ElemSN;

ElemSN *CreatLink(int data[],int num)//创建双向链表
{
int i;
ElemSN *p=NULL,*q=NULL,*h=NULL;

for(i=0;i<num;i++){
    p=(ElemSN *)malloc(sizeof(ElemSN));
    p->next=NULL;
    p->data=data[i];
    if(!h){ //处理头节点 
        h=p;
        h->front=NULL;//头节点的前驱为空 
    }
    else{
        q->next=p;
        p->front=q;
    }
    q=p;
}
return h;

}

ElemSN *FindTail(ElemSN *h)//找到双向链表的尾节点
{
ElemSN *p=h;
while(p->next!=NULL){
p=p->next;
}
return p;
}

void PrintLink(ElemSN *h)//输出双向链表数据域的值
{
ElemSN *p=NULL;
for(p=h;p!=NULL;p=p->next){
printf("%d\t",p->data);
}
printf("\n双向链表顺序输出结束!\n");
}

void BackPrintLink(ElemSN *t)//逆序输出双向链表的值
{
ElemSN *p=NULL;
for(p=t;p!=NULL;p=p->front){
printf("%d\t",p->data);
}
printf("\n双向链表逆序输出结束!\n");
}

void FreeSpace(ElemSN *h)//释放malloc分配的空间
{
int i;
ElemSN *t = NULL;
while(t != NULL){
t = h;
h = h->next;
free(t);
}
}

int main(void)
{
ElemSN *head=NULL,*tail=NULL;
int data[8]={3,2,5,8,4,7,6,9};
int num=8;

head=CreatLink(data,num);
tail=FindTail(head);
printf("顺序输出链表值:\n");
PrintLink(head);
printf("逆序输出链表值:\n");
BackPrintLink(tail);
FreeSpace(head);

return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问