zoukankan      html  css  js  c++  java
  • Anaconda

    #include <stdio.h>
    #include <stdlib.h>

    #define SIZE 1000
    #define THICKNESS 4
    /*
    int run_test(const char snake[SIZE][SIZE]);
    int data[SIZE+2][SIZE+2]={0};
    int run_test(const char snake[SIZE][SIZE])
    {
    for(int i=0;i<SIZE+2;i++){
    for(int j=0;j<SIZE+2;j++){
    data[i][j]=0;
    }
    }
    for(int i=0;i<SIZE;i++){
    for(int j=0;j<SIZE;j++){
    if(snake[i][j]!=0)data[i+1][j+1]++;
    }
    }
    for(int i=1;i<SIZE+1;i++){
    for(int j=1;j<SIZE+1;j++){
    if(data[i][j]==1){
    if(data[i-1][j]+data[i][j-1]+data[i+1][j]+data[i][j+1]==1)data[i][j]=11;
    }
    }
    }
    int count=11;
    bool flag=true;
    while(flag){
    flag=false;
    for(int i=1;i<SIZE+1;i++){
    for(int j=1;j<SIZE+1;j++){
    if(data[i][j]==count){
    if(data[i-1][j]==1){data[i-1][j]=count+1;flag=true;}
    if(data[i+1][j]==1){data[i+1][j]=count+1;flag=true;}
    if(data[i][j-1]==1){data[i][j-1]=count+1;flag=true;}
    if(data[i][j+1]==1){data[i][j+1]=count+1;flag=true;}
    }
    }
    }
    count++;
    }
    count=count-1;
    int z=1;
    for(int i=1;i<SIZE+1;i++){
    for(int j=1;j<SIZE+1;j++){
    if(data[i][j]==count){
    if(data[i-1][j]!=count&&data[i+1][j]!=count&&data[i][j-1]!=count&&data[i][j+1]!=count)z=1;
    else z=0;
    }
    }
    }

    return (count-10)*2-z; // the length of the longest anaconda
    }
    */
    int map[SIZE+2][SIZE+2]={0};
    int run_test(const char snake[SIZE][SIZE])
    {
    for(int i=0;i<SIZE+2;i++){
    for(int j=0;j<SIZE+2;j++){
    map[i][j]=0;
    }
    }
    for(int i=0;i<SIZE;i++){
    for(int j=0;j<SIZE;j++){
    if(snake[i][j]!=0)map[i+1][j+1]++;
    }
    }
    for(int i=1;i<SIZE+2;i++){
    for(int j=1;j<SIZE+2;j++){
    if(map[i][j]==1){
    if(map[i-1][j]+map[i+1][j]+map[i][j-1]+map[i][j+1]==1)map[i][j]=11;

    }
    }
    }
    int count=11;
    int len=0;
    bool s=true;
    while(s){
    s=false;
    for(int i=0;i<SIZE+2;i++){
    for(int j=0;j<SIZE+2;j++){
    if(map[i][j]==count){
    if(map[i-1][j]==1){map[i-1][j]=count+1;s=true;}
    if(map[i+1][j]==1){map[i+1][j]=count+1;s=true;}
    if(map[i][j-1]==1){map[i][j-1]=count+1;s=true;}
    if(map[i][j+1]==1){map[i][j+1]=count+1;s=true;}
    }
    }
    }
    count++;
    }

    count-=1;

    int z=1;
    for(int i=0;i<SIZE+2;i++){
    for(int j=0;j<SIZE+2;j++){
    if(map[i][j]==count){
    if(map[i-1][j]==count)z=0;
    if(map[i+1][j]==count)z=0;
    if(map[i][j-1]==count)z=0;
    if(map[i][j+1]==count)z=0;
    }
    }
    }
    count=2*(count-10)-z;
    return count; // the length of the longest anaconda
    }

    static char snake[10][SIZE][SIZE];

    void build_snake(void)
    {
    for (int l = 0; l < 10; l++)
    {
    for(int y = 0; y < SIZE; y++)
    for(int x = 0; x < SIZE; x++)
    snake[l][x][y] = 0;

    for (int k = 0; k < 99; k++) // limit of snakes
    {
    int flag = 1;
    int curX = (rand() % ((SIZE - THICKNESS * 10) / THICKNESS)) * THICKNESS + THICKNESS * 10;
    int curY = (rand() % ((SIZE - THICKNESS * 10) / THICKNESS)) * THICKNESS + THICKNESS * 10;

    for(int j = -10 * THICKNESS; j < 10 * THICKNESS; j++)
    for(int i = -10 * THICKNESS; i < 10 * THICKNESS; i++)
    if (snake[l][j + curX][i + curY]) flag = 0;

    if (flag)
    {
    for(int i = 0; i < SIZE / 40; i++)
    {
    int dir = rand() % 4;
    int progress = (rand() % 40) * THICKNESS + THICKNESS;

    if (dir == 0) // Right
    {
    for (; curY < progress; curY++)
    if ((snake[l][curX][curY + THICKNESS] + snake[l][curX - 1][curY + THICKNESS] +
    snake[l][curX + 1][curY + THICKNESS]))
    break;
    else
    snake[l][curX][curY] = 1;
    }
    else if (dir == 1) // Left
    {
    for (; curY > progress; curY--)
    if ((snake[l][curX][curY - THICKNESS] + snake[l][curX - 1][curY - THICKNESS] +
    snake[l][curX + 1][curY - THICKNESS]))
    break;
    else
    snake[l][curX][curY] = 1;
    }
    else if (dir == 2) // Up
    {
    for (; curX > progress; curX--)
    if ((snake[l][curX - THICKNESS][curY] + snake[l][curX - THICKNESS][curY - 1] +
    snake[l][curX - THICKNESS][curY + 1]))
    break;
    else
    snake[l][curX][curY] = 1;
    }
    else if (dir == 3) // Down
    {
    for (; curX < progress; curX++)
    if ((snake[l][curX + THICKNESS][curY] + snake[l][curX + THICKNESS][curY - 1] +
    snake[l][curX + THICKNESS][curY + 1]))
    break;
    else
    snake[l][curX][curY] = 1;
    }
    }
    }
    }
    }
    }

    void main(void)
    {
    build_snake();
    for (int count = 0; count < 10; count++)
    printf("%d ", run_test(snake[count]));
    }

  • 相关阅读:
    js的实例方法和静态方法分析
    简述TCP连接的建立与释放(三次握手、四次挥手)
    CSS中各种各样居中方法的总结
    队列的JS实现
    栈的JS实现
    单链表、循环链表的JS实现
    双向链表、双向循环链表的JS实现
    简述JavaScript对象、数组对象与类数组对象
    简述HTML DOM及其节点分类
    关于DOM对象与JQuery对象的那些事
  • 原文地址:https://www.cnblogs.com/ZzznOoooo/p/6628035.html
Copyright © 2011-2022 走看看