栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1482 Partitions

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1482 Partitions

#include <stdio.h> struct part {    struct part* father;    struct part* previous;    int line;    int id;    struct part* next; }grid[2002][2]; int nowline,lastline,total; int n,num; part* temp,*father,*father2; void del(int now,int line) {    int i;    int flag;    for(i = 1;i <= n;i++)       if(grid[i][now].id != 0)       {          flag = 1;          father = grid[i][now].father;          num = father->id;          if(father->next != NULL)          {  temp = father->next;  while(temp->next != NULL)  {     if(temp->line != line)     {        temp->next->previous = temp->previous;        temp->id = 0;        temp->previous->next = temp->next;        --num;     }     else        flag = 0;     temp = temp->next;  }  if(flag)  {     if(temp->line != line)     {        if(father->line != line)        {father->id = 0;++total;        }        temp->id = 0;        father->next = NULL;        father->previous = father;        father->id = 1;     }     else        if(father->line != line)        {father->id = 0;temp->previous = temp;temp->father = temp;temp->id = 1;        }  }  else  {     flag = 1;     if(temp->line != line)     {        temp->id = 0;        temp->previous->next = NULL;        father->id = num - 1;        if(father->line != line)        {father->id = 0;father = father->next;temp = father;while(temp->next != NULL){   temp->father = father;   temp = temp->next;}temp->father = father;father->previous = temp;father->id = num - 2;        }     }     else        if(father->line != line)        {father->id = 0;father = father->next;temp = father;while(temp->next != NULL){   temp->father = father;   temp = temp->next;}temp->father = father;father->previous = temp;father->id = num - 1;        }  }          }          else          {  father->id = 0;  ++total;          }       } } void link1(int row) {    temp = father->previous;    temp->next = &grid[row][nowline];    father->previous = temp->next;    ++father->id;    grid[row][nowline].father = father;    grid[row][nowline].next = NULL;    grid[row][nowline].previous = temp; } void connect(int row) {    if(father->id < father2->id)    {       temp = father;       father = father2;       father2 = temp;    }    father->id += father2->id + 1;    temp = father2->previous;    temp->next = &grid[row][nowline];    grid[row][nowline].next = NULL;    grid[row][nowline].previous = temp;    temp = father->previous;    father2->previous = temp;    temp->next = father2;    temp = father2;    while(temp->next != NULL)    {       temp->father = father;       temp = temp->next;    }    temp->father = father;    father->previous = temp; } int main() {    int i,j;    int temp;    scanf("%d",&n);    nowline = 0;    total = 0;    for(i = 0;i < n + 1;i++)       grid[i][nowline].id = 0;    lastline = nowline;    nowline = 1 - nowline;    for(j = 1;j <= n;j++)    {       grid[0][nowline].id = 0;       for(i = 1;i < n + 1;i++)       {          scanf("%d",&temp);          if(!temp)          {  grid[i][nowline].line = j;  grid[i][nowline].id = 1;  if((grid[i][lastline].id != 0) && (grid[i - 1][nowline].id != 0))  {        if(grid[i][lastline].father != grid[i - 1][nowline].father)     {        father = grid[i][lastline].father;        father2 = grid[i - 1][nowline].father;        connect(i);     }     else     {        father = grid[i][lastline].father;        link1(i);     }  }  else  {     if(grid[i][lastline].id != 0)     {        father = grid[i][lastline].father;        link1(i);     }     else        if(grid[i - 1][nowline].id != 0)        {father = grid[i - 1][nowline].father;link1(i);        }        else        {grid[i][nowline].father = &grid[i][nowline];grid[i][nowline].next = NULL;grid[i][nowline].previous = &grid[i][nowline];        }  }          }          else  grid[i][nowline].id = 0;       }       del(lastline,j);       lastline = nowline;       nowline = 1 - nowline;    }    for(i = 0;i < n + 1;i++)       grid[i][nowline].id = 0;    del(lastline,j);    printf("%dn",total);    return 0; }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号