The problem of moving tables with greedy algorithm

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceseqlistsort{/// <summary>    /// <ather>    ///Lihonlin/// </ather>    /// <content>    ///The problem of moving tables///A teaching building has n classrooms on the first floor, numbered 1, 2 、...、 n from left to right. Now we have to move some desks from some classrooms///in other classrooms, each table is moved from a smaller classroom to a numbered classroom, and every trip goes from left to right,///after you have finished moving a desk, you can continue to move from the current position or to the right to take another table. Input data: First enter N, M, and then immediately///The m-line enters the starting classroom and target classroom for the M-card to move the desks.    Output data: You need to run a few trips at least. ///Sample Input///Ten 5///1 3///3 9///4 6///6 Ten///7 8///Sample Output///3/// </content>    /// </summary>     Public structThe hostel { Public intstart;  Public intend; }    classGreedy_desk {Const intSize = -; Static intm; Static intN; StaticRoom[] TheNewRoom[size]; Static BOOL[] Ismove =New BOOL[size]; Static intMin =0; Static voidInitData () {Console.WriteLine ("Please enter n classrooms"); N=Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Please enter the M-line desk"); M=Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Enter the starting classroom and target classroom for the M-card to move the desks");  for(inti =0;  I < m; ++i) {Room[i].start=Convert.ToInt32 (Console.ReadLine ()); Room[i].end=Convert.ToInt32 (Console.ReadLine ()); }                    }        Static voidSort () { for(inti =0; I < m; ++i) { for(intj =0; J < M-i-1; ++j) {if(Room[j].start > Room[j +1].start) {inttemp =Room[j].start; Room[j].start= Room[j +1].start; Room[j+1].start =temp; }                }            }        }         Public Static voidMovedesk () {initdata ();            Sort (); intnum =0;  while(Num <m) {inttemp =0;  for(inti =0; I < m; ++i) {if(Temp <= room[i].start &&!)Ismove[i]) {Temp=Room[i].end; Ismove[i]=true; Num++; }} min++; } Console.WriteLine ("minimum run of {0} trips required", Min); }    }}

The problem of moving tables with greedy algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.