IOS development-cinema seat selection algorithm restricts the generation of isolated seats, ios cinema
As we all know, currently, you are not allowed to choose any seats for a cinema. We cannot choose or choose to choose from any other seats. Finally, we can leave N separate seats in order to leave at least two connected seats;
In addition, seats in some cinemas are not arranged in a regular manner. Some seats are separated by aisles or special seats, resulting in different partitions. In this case, we can check whether there are any seats that have become isolated seats,
Simply put, two couples cannot sit next to each other ~
However, there are also special circumstances, and because of the partition, there are only two or only three seats in a row, at this time, people can still buy one or two of them ~
After the conditions are met, we will introduce the data structure. Normally, each seat data in the cinema will contain coordinates, seat numbers, such as x, y, row, and column.
The area in the movie hall is divided into M * N coordinate systems based on the size of a single seat, that is, the aisle also has its coordinates. row and column are the real seats;
For a long time, the following code is displayed:
The entry function is verifySelectedSeatsWithSeatsDic,
1. The parameter seatsDic is a dictionary. Note that this dictionary filters out non-seat data! (The dictionary is required for this project. You can use an array directly. In the method, I also obtain the array. Note that the sequence of the seat data must be ordered, in this way, the left and right seats of a seat can be obtained)
2. I encapsulated the data for each seat into a WCSeatButton and provided the isSeatAvailable method to determine whether the seat can be purchased (not sold)
3. The getNearBySeatsInSameRowForSeat function is used to obtain all seats not separated by aisles and other non-seats.
4. function (BOOL) isSeat :( WCSeatButton *) s1 nearBySeatWithoutRoad :( WCSeatButton *) s2 checks whether two seats are separated by aisles
-(BOOL) verifySelectedSeatsWithSeatsDic :( NSDictionary *) seatsDic {NSArray * seatBtns = [seatsDic allValues]; if ([seatBtns count]> 0) {for (WCSeatButton * btn seatBtns) {// check whether the available seats are listed in the ticket if ([btn isSeatAvailable]) {int idx = btn. seatIndex; WCSeatButton * preBtn = [seatsDic objectForKey: [NSString stringWithFormat: @ "% I", idx-1]; WCSeatButton * nextBtn = [seatsDic objectForKey: [NSString stringWithForm At: @ "% I", idx + 1]; // check whether adjacent seats exist and whether BOOL isPreOK = preBtn is available! = Nil & [preBtn. serviceSeat. row isw.tostring: btn. serviceSeat. row] & [preBtn isSeatAvailable]; BOOL isNextOK = nextBtn! = Nil & [nextBtn. serviceSeat. row isw.tostring: btn. serviceSeat. row] & [nextBtn isSeatAvailable]; // determine if (isPreOK) {isPreOK = ABS ([btn. serviceSeat. yCoord intValue]-[preBtn. serviceSeat. yCoord intValue]) = 1;} if (isNextOK) {isNextOK = ABS ([btn. serviceSeat. yCoord intValue]-[nextBtn. serviceSeat. yCoord intValue]) = 1;} if (! IsPreOK &&! IsNextOK) {NSArray * nearBySeats = [self Testing: btn withSeatsDic: seatsDic]; // select only one if ([nearBySeats count] = 2) {continue ;} // two out of the three remaining seats are selected consecutively. if ([nearBySeats count] = 3) {int idx = [nearBySeats indexOfObject: btn]; // if there is an empty seat on either side indicating that the middle seat has been selected, you can select if (idx = 0 &&! [NearBySeats [2] isSeatAvailable]) {continue;} else if (idx = 2 &&! [NearBySeats [0] isSeatAvailable]) {continue ;}// only the seats selected by the current user are restricted, this prevents the current user from selecting a seat for (WC630ServiceSeat * s in self. orderItem. seats) {if (preBtn & [[[preBtn serviceSeat] cineSeatId] isEqualToString: s. cineSeatId]) | (nextBtn & [[nextBtn serviceSeat] cineSeatId] isEqualToString: s. cineSeatId]) {return NO ;}}}} return YES ;}- (NSArray *) getNearBySeatsInSameRowForSeat :( WCSeatButton *) sea T withSeatsDic :( NSDictionary *) seatsDic {NSMutableArray * result = [NSMutableArray array]; [result addObject: seat]; int idx = seat. seatIndex-1; // left WCSeatButton * tmp = [seatsDic objectForKey: [NSString stringWithFormat: @ "% I", idx]; while ([self isSeat: tmp nearBySeatWithoutRoad: seat]) {[result insertObject: tmp atIndex: 0]; idx --; tmp = [seatsDic objectForKey: [NSString stringWithFormat: @ "% I", idx] ;} Idx = seat. seatIndex + 1; // right tmp = [seatsDic objectForKey: [NSString stringWithFormat: @ "% I", idx]; while ([self isSeat: tmp nearBySeatWithoutRoad: seat]) {[result addObject: tmp]; idx ++; tmp = [seatsDic objectForKey: [NSString stringWithFormat: @ "% I", idx];} return result ;} -(BOOL) isSeat :( WCSeatButton *) s1 nearBySeatWithoutRoad :( WCSeatButton *) s2 {return s1! = Nil & [s1.serviceSeat. row isEqualToString: s2.serviceSeat. row] & ABS ([s1.serviceSeat. yCoord intValue]-[s2.serviceSeat. yCoord intValue]) = ABS ([s1.serviceSeat. column intValue]-[s2.serviceSeat. column intValue]);}